Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
|
Jennifer Messerly
2016/02/09 01:21:33
2016? :)
ochafik
2016/02/10 18:12:45
Time flies!
| |
| 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. | |
| 4 | |
| 5 part of js_ast; | |
| 6 | |
| 7 final _any = new AnyTypeRef._(); | |
| 8 final _unknown = new UnknownTypeRef._(); | |
| 9 final _null = new NullTypeRef(); | |
| 10 | |
| 11 /// JavaScript type reference. | |
|
Jennifer Messerly
2016/02/09 01:21:33
It'd be good to add more info here. Like maybe a q
ochafik
2016/02/10 18:12:45
Done.
| |
| 12 abstract class TypeRef extends Expression { | |
| 13 static final _namedCache = <String, TypeRef>{}; | |
|
Jennifer Messerly
2016/02/09 01:21:33
Is this an optimization or is canonicalization a r
ochafik
2016/02/10 18:12:45
Dropped the premature opt, I'm not testing for typ
| |
| 14 | |
| 15 int get precedenceLevel => EXPRESSION; | |
|
Jennifer Messerly
2016/02/09 01:21:33
this seems like a really low precedence. I'd expec
ochafik
2016/02/10 18:12:45
Done.
| |
| 16 | |
| 17 TypeRef(); | |
|
Jennifer Messerly
2016/02/09 01:21:33
Is this needed? I think you'll get it for free
ochafik
2016/02/10 18:12:45
As soon as there's a factory ctor, seems we don't
| |
| 18 | |
| 19 factory TypeRef.any() => _any; | |
| 20 | |
| 21 factory TypeRef.void_() => new TypeRef.named('void'); | |
| 22 | |
| 23 factory TypeRef.unknown() => _unknown; | |
| 24 | |
| 25 factory TypeRef.generic(TypeRef rawType, Iterable<TypeRef> typeParams) => | |
| 26 typeParams.isEmpty | |
| 27 ? rawType | |
| 28 : new GenericTypeRef(rawType, typeParams.toList()); | |
|
Jennifer Messerly
2016/02/09 16:41:57
looking at these factories again ... I'd probably
ochafik
2016/02/10 18:12:45
Done.
| |
| 29 | |
| 30 factory TypeRef.array([TypeRef elementType]) => | |
| 31 elementType == null ? new TypeRef.named('Array') : new ArrayTypeRef(elemen tType); | |
|
Jennifer Messerly
2016/02/09 01:21:33
long line
Jennifer Messerly
2016/02/09 16:41:57
Another thought here. I noticed in the printing co
ochafik
2016/02/10 18:12:45
Done.
ochafik
2016/02/10 18:12:45
Done for Array & Function. Left a TODO for object
| |
| 32 | |
| 33 factory TypeRef.object([TypeRef keyType, TypeRef valueType]) { | |
| 34 var rawType = new TypeRef.named('Object'); | |
| 35 return keyType == null && valueType == null | |
| 36 ? rawType | |
| 37 : new GenericTypeRef(rawType, [keyType ?? _any, valueType ?? _any]); | |
| 38 } | |
| 39 | |
| 40 factory TypeRef.function([TypeRef returnType, Map<Identifier, TypeRef> paramTy pes]) => | |
|
Jennifer Messerly
2016/02/09 01:21:33
long line here too
ochafik
2016/02/10 18:12:45
Sorry, formatter strangely didn't tackle that one.
| |
| 41 returnType == null && paramTypes == null | |
| 42 ? new TypeRef.named('Function') | |
| 43 : new FunctionTypeRef(returnType, paramTypes); | |
| 44 | |
| 45 factory TypeRef.record(Map<Identifier, TypeRef> types) => | |
| 46 new RecordTypeRef(types); | |
| 47 | |
| 48 factory TypeRef.string() => new TypeRef.named('string'); | |
| 49 | |
| 50 factory TypeRef.number() => new TypeRef.named('number'); | |
| 51 | |
| 52 factory TypeRef.undefined() => new TypeRef.named('undefined'); | |
| 53 | |
| 54 factory TypeRef.boolean() => new TypeRef.named('boolean'); | |
| 55 | |
| 56 factory TypeRef.qualified(List<Identifier> path) => | |
| 57 _namedCache.putIfAbsent( | |
| 58 path.map((p) => p.name).join('.'), | |
| 59 () => new QualifiedTypeRef(path)); | |
| 60 | |
| 61 factory TypeRef.named(String name) => | |
| 62 new TypeRef.qualified(<Identifier>[new Identifier(name)]); | |
| 63 | |
| 64 bool get isAny => this is AnyTypeRef; | |
| 65 bool get isUnknown => this is UnknownTypeRef; | |
| 66 bool get isNull => this is NullTypeRef; | |
| 67 | |
| 68 TypeRef or(TypeRef other) => new UnionTypeRef([this, other]); | |
| 69 | |
| 70 TypeRef orUndefined() => or(new TypeRef.undefined()); | |
| 71 TypeRef orNull() => or(_null); | |
| 72 | |
| 73 TypeRef toOptional() => | |
| 74 new OptionalTypeRef(this); | |
| 75 } | |
| 76 | |
| 77 class AnyTypeRef extends TypeRef { | |
| 78 AnyTypeRef._() : super(); | |
| 79 | |
| 80 factory AnyTypeRef() => _any; | |
| 81 accept(NodeVisitor visitor) => visitor.visitAnyTypeRef(this); | |
| 82 void visitChildren(NodeVisitor visitor) {} | |
| 83 _clone() => new AnyTypeRef(); | |
| 84 } | |
| 85 | |
| 86 class NullTypeRef extends QualifiedTypeRef { | |
| 87 NullTypeRef() : super([new Identifier("null")]); | |
| 88 _clone() => new NullTypeRef(); | |
| 89 } | |
| 90 | |
| 91 class UnknownTypeRef extends TypeRef { | |
| 92 UnknownTypeRef._() : super(); | |
| 93 | |
| 94 factory UnknownTypeRef() => _unknown; | |
| 95 accept(NodeVisitor visitor) => visitor.visitUnknownTypeRef(this); | |
| 96 void visitChildren(NodeVisitor visitor) {} | |
| 97 _clone() => new UnknownTypeRef(); | |
| 98 } | |
| 99 | |
| 100 class QualifiedTypeRef extends TypeRef { | |
| 101 final List<Identifier> path; | |
| 102 QualifiedTypeRef(this.path); | |
| 103 | |
| 104 accept(NodeVisitor visitor) => visitor.visitQualifiedTypeRef(this); | |
| 105 void visitChildren(NodeVisitor visitor) => | |
| 106 path.forEach((p) => p.accept(visitor)); | |
| 107 _clone() => new QualifiedTypeRef(path); | |
| 108 } | |
| 109 | |
| 110 class ArrayTypeRef extends TypeRef { | |
| 111 final TypeRef elementType; | |
| 112 ArrayTypeRef(this.elementType); | |
| 113 accept(NodeVisitor visitor) => visitor.visitArrayTypeRef(this); | |
| 114 void visitChildren(NodeVisitor visitor) { | |
| 115 elementType.accept(visitor); | |
| 116 } | |
| 117 _clone() => new ArrayTypeRef(elementType); | |
| 118 } | |
| 119 | |
| 120 class GenericTypeRef extends TypeRef { | |
| 121 final TypeRef rawType; | |
| 122 final List<TypeRef> typeParams; | |
| 123 GenericTypeRef(this.rawType, this.typeParams); | |
| 124 | |
| 125 accept(NodeVisitor visitor) => visitor.visitGenericTypeRef(this); | |
| 126 void visitChildren(NodeVisitor visitor) { | |
| 127 rawType.accept(visitor); | |
| 128 typeParams.forEach((p) => p.accept(visitor)); | |
| 129 } | |
| 130 _clone() => new GenericTypeRef(rawType, typeParams); | |
| 131 } | |
| 132 | |
| 133 class UnionTypeRef extends TypeRef { | |
| 134 final List<TypeRef> types; | |
| 135 UnionTypeRef(this.types); | |
| 136 | |
| 137 accept(NodeVisitor visitor) => visitor.visitUnionTypeRef(this); | |
| 138 void visitChildren(NodeVisitor visitor) { | |
| 139 types.forEach((p) => p.accept(visitor)); | |
| 140 } | |
| 141 _clone() => new UnionTypeRef(types); | |
| 142 | |
| 143 @override | |
| 144 TypeRef or(TypeRef other) { | |
| 145 if (types.contains(other)) return this; | |
| 146 return new UnionTypeRef([]..addAll(types)..add(other)); | |
| 147 } | |
| 148 } | |
| 149 | |
| 150 class OptionalTypeRef extends TypeRef { | |
| 151 final TypeRef type; | |
| 152 OptionalTypeRef(this.type); | |
| 153 | |
| 154 accept(NodeVisitor visitor) => visitor.visitOptionalTypeRef(this); | |
| 155 void visitChildren(NodeVisitor visitor) { | |
| 156 type.accept(visitor); | |
| 157 } | |
| 158 _clone() => new OptionalTypeRef(type); | |
| 159 | |
| 160 @override | |
| 161 TypeRef orUndefined() => this; | |
| 162 } | |
| 163 | |
| 164 class RecordTypeRef extends TypeRef { | |
| 165 final Map<Identifier, TypeRef> types; | |
| 166 RecordTypeRef(this.types); | |
| 167 | |
| 168 accept(NodeVisitor visitor) => visitor.visitRecordTypeRef(this); | |
| 169 void visitChildren(NodeVisitor visitor) { | |
| 170 types.values.forEach((p) => p.accept(visitor)); | |
| 171 } | |
| 172 _clone() => new RecordTypeRef(types); | |
| 173 } | |
| 174 | |
| 175 class FunctionTypeRef extends TypeRef { | |
| 176 final TypeRef returnType; | |
| 177 final Map<Identifier, TypeRef> paramTypes; | |
| 178 FunctionTypeRef(this.returnType, this.paramTypes); | |
| 179 | |
| 180 accept(NodeVisitor visitor) => visitor.visitFunctionTypeRef(this); | |
| 181 void visitChildren(NodeVisitor visitor) { | |
| 182 returnType.accept(visitor); | |
| 183 paramTypes.forEach((n, t) { | |
| 184 n.accept(visitor); | |
| 185 t.accept(visitor); | |
| 186 }); | |
| 187 } | |
| 188 _clone() => new FunctionTypeRef(returnType, paramTypes); | |
| 189 } | |
| OLD | NEW |