| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 js_ast; | 5 part of js_ast; |
| 6 | 6 |
| 7 final _any = new AnyTypeRef._(); | 7 final _any = new AnyTypeRef._(); |
| 8 final _unknown = new UnknownTypeRef._(); | 8 final _unknown = new UnknownTypeRef._(); |
| 9 final _null = new NullTypeRef(); | 9 final _null = new NullTypeRef(); |
| 10 | 10 |
| 11 /// JavaScript type reference, designed to support a subset of the type systems | 11 /// JavaScript type reference, designed to support a subset of the type systems |
| 12 /// of the Closure Compiler and TypeScript: | 12 /// of the Closure Compiler and TypeScript: |
| 13 /// - https://developers.google.com/closure/compiler/docs/js-for-compiler#types | 13 /// - https://developers.google.com/closure/compiler/docs/js-for-compiler#types |
| 14 /// - https://github.com/Microsoft/TypeScript/blob/v1.8.0-beta/doc/spec.md#3 | 14 /// - https://github.com/Microsoft/TypeScript/blob/v1.8.0-beta/doc/spec.md#3 |
| 15 /// | 15 /// |
| 16 /// Note that some subtleties like "nullability" or "optionality" are handled | 16 /// Note that some subtleties like "nullability" or "optionality" are handled |
| 17 /// using unions (with a [NullTypeRef] or with an "undefined" named typeref). | 17 /// using unions (with a [NullTypeRef] or with an "undefined" named typeref). |
| 18 /// Also, primitives aren't modeled differently than named / qualified types, | 18 /// Also, primitives aren't modeled differently than named / qualified types, |
| 19 /// as it brings little value for now. Primitive-specific type formatting is | 19 /// as it brings little value for now. Primitive-specific type formatting is |
| 20 /// handled by the type printers (for instance, the knowledge that | 20 /// handled by the type printers (for instance, the knowledge that |
| 21 /// `number|null` is just `number` in TypeScript, and is `number?` in Closure). | 21 /// `number|null` is just `number` in TypeScript, and is `number?` in Closure). |
| 22 abstract class TypeRef extends Expression { | 22 abstract class TypeRef extends Expression { |
| 23 | |
| 24 int get precedenceLevel => PRIMARY; | 23 int get precedenceLevel => PRIMARY; |
| 25 | 24 |
| 26 TypeRef(); | 25 TypeRef(); |
| 27 | 26 |
| 28 factory TypeRef.any() => _any; | 27 factory TypeRef.any() => _any; |
| 29 | 28 |
| 30 factory TypeRef.void_() => new TypeRef.named('void'); | 29 factory TypeRef.void_() => new TypeRef.named('void'); |
| 31 | 30 |
| 32 factory TypeRef.unknown() => _unknown; | 31 factory TypeRef.unknown() => _unknown; |
| 33 | 32 |
| 34 factory TypeRef.generic(TypeRef rawType, Iterable<TypeRef> typeArgs) { | 33 factory TypeRef.generic(TypeRef rawType, Iterable<TypeRef> typeArgs) { |
| 35 if (typeArgs.isEmpty) { | 34 if (typeArgs.isEmpty) { |
| 36 throw new ArgumentError.value(typeArgs, "typeArgs", "is empty"); | 35 throw new ArgumentError.value(typeArgs, "typeArgs", "is empty"); |
| 37 } | 36 } |
| 38 return new GenericTypeRef(rawType, typeArgs.toList()); | 37 return new GenericTypeRef(rawType, typeArgs.toList()); |
| 39 } | 38 } |
| 40 | 39 |
| 41 factory TypeRef.array([TypeRef elementType]) => | 40 factory TypeRef.array([TypeRef elementType]) => new ArrayTypeRef(elementType); |
| 42 new ArrayTypeRef(elementType); | |
| 43 | 41 |
| 44 factory TypeRef.object([TypeRef keyType, TypeRef valueType]) { | 42 factory TypeRef.object([TypeRef keyType, TypeRef valueType]) { |
| 45 // TODO(ochafik): Roll out a dedicated ObjectTypeRef? | 43 // TODO(ochafik): Roll out a dedicated ObjectTypeRef? |
| 46 var rawType = new TypeRef.named('Object'); | 44 var rawType = new TypeRef.named('Object'); |
| 47 return keyType == null && valueType == null | 45 return keyType == null && valueType == null |
| 48 ? rawType | 46 ? rawType |
| 49 : new GenericTypeRef(rawType, [keyType ?? _any, valueType ?? _any]); | 47 : new GenericTypeRef(rawType, [keyType ?? _any, valueType ?? _any]); |
| 50 } | 48 } |
| 51 | 49 |
| 52 factory TypeRef.function( | 50 factory TypeRef.function( |
| 53 [TypeRef returnType, Map<Identifier, TypeRef> paramTypes]) => | 51 [TypeRef returnType, Map<Identifier, TypeRef> paramTypes]) => |
| 54 new FunctionTypeRef(returnType, paramTypes); | 52 new FunctionTypeRef(returnType, paramTypes); |
| 55 | 53 |
| 56 factory TypeRef.record(Map<Identifier, TypeRef> types) => | 54 factory TypeRef.record(Map<Identifier, TypeRef> types) => |
| 57 new RecordTypeRef(types); | 55 new RecordTypeRef(types); |
| 58 | 56 |
| 59 factory TypeRef.string() => new TypeRef.named('string'); | 57 factory TypeRef.string() => new TypeRef.named('string'); |
| 60 | 58 |
| 61 factory TypeRef.number() => new TypeRef.named('number'); | 59 factory TypeRef.number() => new TypeRef.named('number'); |
| 62 | 60 |
| 63 factory TypeRef.undefined() => new TypeRef.named('undefined'); | 61 factory TypeRef.undefined() => new TypeRef.named('undefined'); |
| 64 | 62 |
| 65 factory TypeRef.boolean() => new TypeRef.named('boolean'); | 63 factory TypeRef.boolean() => new TypeRef.named('boolean'); |
| 66 | 64 |
| 67 factory TypeRef.qualified(List<Identifier> path) => | 65 factory TypeRef.qualified(List<Identifier> path) => |
| 68 new QualifiedTypeRef(path); | 66 new QualifiedTypeRef(path); |
| 69 | 67 |
| 70 factory TypeRef.named(String name) => | 68 factory TypeRef.named(String name) => |
| 71 new TypeRef.qualified(<Identifier>[new Identifier(name)]); | 69 new TypeRef.qualified(<Identifier>[new Identifier(name)]); |
| 72 | 70 |
| 73 bool get isAny => this is AnyTypeRef; | 71 bool get isAny => this is AnyTypeRef; |
| 74 bool get isUnknown => this is UnknownTypeRef; | 72 bool get isUnknown => this is UnknownTypeRef; |
| 75 bool get isNull => this is NullTypeRef; | 73 bool get isNull => this is NullTypeRef; |
| 76 | 74 |
| 77 TypeRef or(TypeRef other) => new UnionTypeRef([this, other]); | 75 TypeRef or(TypeRef other) => new UnionTypeRef([this, other]); |
| 78 | 76 |
| 79 TypeRef orUndefined() => or(new TypeRef.undefined()); | 77 TypeRef orUndefined() => or(new TypeRef.undefined()); |
| 80 TypeRef orNull() => or(_null); | 78 TypeRef orNull() => or(_null); |
| 81 | 79 |
| 82 TypeRef toOptional() => | 80 TypeRef toOptional() => new OptionalTypeRef(this); |
| 83 new OptionalTypeRef(this); | |
| 84 } | 81 } |
| 85 | 82 |
| 86 class AnyTypeRef extends TypeRef { | 83 class AnyTypeRef extends TypeRef { |
| 87 AnyTypeRef._() : super(); | 84 AnyTypeRef._() : super(); |
| 88 | 85 |
| 89 factory AnyTypeRef() => _any; | 86 factory AnyTypeRef() => _any; |
| 90 accept(NodeVisitor visitor) => visitor.visitAnyTypeRef(this); | 87 accept(NodeVisitor visitor) => visitor.visitAnyTypeRef(this); |
| 91 void visitChildren(NodeVisitor visitor) {} | 88 void visitChildren(NodeVisitor visitor) {} |
| 92 _clone() => new AnyTypeRef(); | 89 _clone() => new AnyTypeRef(); |
| 93 } | 90 } |
| (...skipping 22 matching lines...) Expand all Loading... |
| 116 _clone() => new QualifiedTypeRef(path); | 113 _clone() => new QualifiedTypeRef(path); |
| 117 } | 114 } |
| 118 | 115 |
| 119 class ArrayTypeRef extends TypeRef { | 116 class ArrayTypeRef extends TypeRef { |
| 120 final TypeRef elementType; | 117 final TypeRef elementType; |
| 121 ArrayTypeRef(this.elementType); | 118 ArrayTypeRef(this.elementType); |
| 122 accept(NodeVisitor visitor) => visitor.visitArrayTypeRef(this); | 119 accept(NodeVisitor visitor) => visitor.visitArrayTypeRef(this); |
| 123 void visitChildren(NodeVisitor visitor) { | 120 void visitChildren(NodeVisitor visitor) { |
| 124 elementType.accept(visitor); | 121 elementType.accept(visitor); |
| 125 } | 122 } |
| 123 |
| 126 _clone() => new ArrayTypeRef(elementType); | 124 _clone() => new ArrayTypeRef(elementType); |
| 127 } | 125 } |
| 128 | 126 |
| 129 class GenericTypeRef extends TypeRef { | 127 class GenericTypeRef extends TypeRef { |
| 130 final TypeRef rawType; | 128 final TypeRef rawType; |
| 131 final List<TypeRef> typeArgs; | 129 final List<TypeRef> typeArgs; |
| 132 GenericTypeRef(this.rawType, this.typeArgs); | 130 GenericTypeRef(this.rawType, this.typeArgs); |
| 133 | 131 |
| 134 accept(NodeVisitor visitor) => visitor.visitGenericTypeRef(this); | 132 accept(NodeVisitor visitor) => visitor.visitGenericTypeRef(this); |
| 135 void visitChildren(NodeVisitor visitor) { | 133 void visitChildren(NodeVisitor visitor) { |
| 136 rawType.accept(visitor); | 134 rawType.accept(visitor); |
| 137 typeArgs.forEach((p) => p.accept(visitor)); | 135 typeArgs.forEach((p) => p.accept(visitor)); |
| 138 } | 136 } |
| 137 |
| 139 _clone() => new GenericTypeRef(rawType, typeArgs); | 138 _clone() => new GenericTypeRef(rawType, typeArgs); |
| 140 } | 139 } |
| 141 | 140 |
| 142 class UnionTypeRef extends TypeRef { | 141 class UnionTypeRef extends TypeRef { |
| 143 final List<TypeRef> types; | 142 final List<TypeRef> types; |
| 144 UnionTypeRef(this.types); | 143 UnionTypeRef(this.types); |
| 145 | 144 |
| 146 accept(NodeVisitor visitor) => visitor.visitUnionTypeRef(this); | 145 accept(NodeVisitor visitor) => visitor.visitUnionTypeRef(this); |
| 147 void visitChildren(NodeVisitor visitor) { | 146 void visitChildren(NodeVisitor visitor) { |
| 148 types.forEach((p) => p.accept(visitor)); | 147 types.forEach((p) => p.accept(visitor)); |
| 149 } | 148 } |
| 149 |
| 150 _clone() => new UnionTypeRef(types); | 150 _clone() => new UnionTypeRef(types); |
| 151 | 151 |
| 152 @override | 152 @override |
| 153 TypeRef or(TypeRef other) { | 153 TypeRef or(TypeRef other) { |
| 154 if (types.contains(other)) return this; | 154 if (types.contains(other)) return this; |
| 155 return new UnionTypeRef([]..addAll(types)..add(other)); | 155 return new UnionTypeRef([] |
| 156 ..addAll(types) |
| 157 ..add(other)); |
| 156 } | 158 } |
| 157 } | 159 } |
| 158 | 160 |
| 159 class OptionalTypeRef extends TypeRef { | 161 class OptionalTypeRef extends TypeRef { |
| 160 final TypeRef type; | 162 final TypeRef type; |
| 161 OptionalTypeRef(this.type); | 163 OptionalTypeRef(this.type); |
| 162 | 164 |
| 163 accept(NodeVisitor visitor) => visitor.visitOptionalTypeRef(this); | 165 accept(NodeVisitor visitor) => visitor.visitOptionalTypeRef(this); |
| 164 void visitChildren(NodeVisitor visitor) { | 166 void visitChildren(NodeVisitor visitor) { |
| 165 type.accept(visitor); | 167 type.accept(visitor); |
| 166 } | 168 } |
| 169 |
| 167 _clone() => new OptionalTypeRef(type); | 170 _clone() => new OptionalTypeRef(type); |
| 168 | 171 |
| 169 @override | 172 @override |
| 170 TypeRef orUndefined() => this; | 173 TypeRef orUndefined() => this; |
| 171 } | 174 } |
| 172 | 175 |
| 173 class RecordTypeRef extends TypeRef { | 176 class RecordTypeRef extends TypeRef { |
| 174 final Map<Identifier, TypeRef> types; | 177 final Map<Identifier, TypeRef> types; |
| 175 RecordTypeRef(this.types); | 178 RecordTypeRef(this.types); |
| 176 | 179 |
| 177 accept(NodeVisitor visitor) => visitor.visitRecordTypeRef(this); | 180 accept(NodeVisitor visitor) => visitor.visitRecordTypeRef(this); |
| 178 void visitChildren(NodeVisitor visitor) { | 181 void visitChildren(NodeVisitor visitor) { |
| 179 types.values.forEach((p) => p.accept(visitor)); | 182 types.values.forEach((p) => p.accept(visitor)); |
| 180 } | 183 } |
| 184 |
| 181 _clone() => new RecordTypeRef(types); | 185 _clone() => new RecordTypeRef(types); |
| 182 } | 186 } |
| 183 | 187 |
| 184 class FunctionTypeRef extends TypeRef { | 188 class FunctionTypeRef extends TypeRef { |
| 185 final TypeRef returnType; | 189 final TypeRef returnType; |
| 186 final Map<Identifier, TypeRef> paramTypes; | 190 final Map<Identifier, TypeRef> paramTypes; |
| 187 FunctionTypeRef(this.returnType, this.paramTypes); | 191 FunctionTypeRef(this.returnType, this.paramTypes); |
| 188 | 192 |
| 189 accept(NodeVisitor visitor) => visitor.visitFunctionTypeRef(this); | 193 accept(NodeVisitor visitor) => visitor.visitFunctionTypeRef(this); |
| 190 void visitChildren(NodeVisitor visitor) { | 194 void visitChildren(NodeVisitor visitor) { |
| 191 returnType.accept(visitor); | 195 returnType.accept(visitor); |
| 192 paramTypes.forEach((n, t) { | 196 paramTypes.forEach((n, t) { |
| 193 n.accept(visitor); | 197 n.accept(visitor); |
| 194 t.accept(visitor); | 198 t.accept(visitor); |
| 195 }); | 199 }); |
| 196 } | 200 } |
| 201 |
| 197 _clone() => new FunctionTypeRef(returnType, paramTypes); | 202 _clone() => new FunctionTypeRef(returnType, paramTypes); |
| 198 } | 203 } |
| OLD | NEW |