-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
EnumType.java
237 lines (210 loc) · 6.9 KB
/
EnumType.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
* Copyright 2014 The Closure Compiler Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.javascript.jscomp.newtypes;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import com.google.javascript.rhino.JSTypeExpression;
import com.google.javascript.rhino.Node;
import java.util.Collection;
/**
*
* @author blickly@google.com (Ben Lickly)
* @author dimvar@google.com (Dimitris Vardoulakis)
*
* Represents an enumerated type.
* Each enum declaration produces two types of interest:
* - We represent the object literal that defined the enum as an ObjectType.
* - We represent an element of the enum by using this class in JSType.
*/
public final class EnumType extends Namespace implements TypeWithProperties {
private enum State {
NOT_RESOLVED,
DURING_RESOLUTION,
RESOLVED
}
private State state;
private JSTypeExpression typeExpr;
// The type that accompanies the enum declaration
private JSType declaredType;
// The type of the enum's properties, a subtype of the previous field.
private JSType enumPropType;
// All properties have the same type, so we only need a set, not a map.
private ImmutableSet<String> props;
private EnumType(JSTypes commonTypes, String name, Node defSite,
JSTypeExpression typeExpr, Collection<String> props) {
super(commonTypes, name, defSite);
Preconditions.checkNotNull(typeExpr);
this.state = State.NOT_RESOLVED;
// typeExpr is non-null iff the enum is not resolved
this.typeExpr = typeExpr;
this.props = ImmutableSet.copyOf(props);
}
public static EnumType make(JSTypes commonTypes, String name, Node defSite,
JSTypeExpression typeExpr, Collection<String> props) {
return new EnumType(commonTypes, name, defSite, typeExpr, props);
}
public boolean isResolved() {
return this.state == State.RESOLVED;
}
JSTypes getCommonTypes() {
return this.commonTypes;
}
public JSType getEnumeratedType() {
Preconditions.checkState(this.state == State.RESOLVED);
return declaredType;
}
public JSType getPropType() {
Preconditions.checkState(this.state == State.RESOLVED);
return enumPropType;
}
// Returns null iff there is a type cycle
public JSTypeExpression getTypeExpr() {
Preconditions.checkState(this.state != State.RESOLVED);
if (this.state == State.DURING_RESOLUTION) {
return null;
}
this.state = State.DURING_RESOLUTION;
return typeExpr;
}
public JSTypeExpression getTypeExprForErrorReporting() {
Preconditions.checkState(this.state == State.DURING_RESOLUTION);
return typeExpr;
}
void resolveEnum(JSType t) {
Preconditions.checkNotNull(t);
if (this.state == State.RESOLVED) {
return;
}
Preconditions.checkState(this.state == State.DURING_RESOLUTION,
"Expected state DURING_RESOLUTION but found %s", this.state.toString());
this.state = State.RESOLVED;
typeExpr = null;
declaredType = t;
enumPropType = JSType.fromEnum(this);
}
/**
* When defining an enum such as
* /** @enum {number} * /
* var X = { ONE: 1, TWO: 2 };
* the properties of the object literal are constant.
*/
@Override
protected JSType computeJSType() {
Preconditions.checkNotNull(enumPropType);
Preconditions.checkState(this.namespaceType == null);
PersistentMap<String, Property> propMap = PersistentMap.create();
for (String s : this.props) {
propMap = propMap.with(s,
Property.makeConstant(null, enumPropType, enumPropType));
}
return JSType.fromObjectType(ObjectType.makeObjectType(
this.commonTypes, this.commonTypes.getLiteralObjNominalType(), propMap,
null, this, false, ObjectKind.UNRESTRICTED));
}
@Override
public JSType getProp(QualifiedName qname) {
return declaredType.getProp(qname);
}
@Override
public JSType getDeclaredProp(QualifiedName qname) {
return declaredType.getDeclaredProp(qname);
}
@Override
public boolean mayHaveProp(QualifiedName qname) {
return declaredType.mayHaveProp(qname);
}
@Override
public boolean hasProp(QualifiedName qname) {
return declaredType.hasProp(qname);
}
@Override
public boolean hasConstantProp(QualifiedName qname) {
return declaredType.hasConstantProp(qname);
}
// Unlike hasProp, this method asks about the object literal in the enum
// definition, not about the declared type of the enum.
public boolean enumLiteralHasKey(String name) {
return props.contains(name);
}
static boolean hasScalar(ImmutableSet<EnumType> enums) {
for (EnumType e : enums) {
if (e.declaredType.hasScalar()) {
return true;
}
}
return false;
}
static boolean hasNonScalar(ImmutableSet<EnumType> enums) {
for (EnumType e : enums) {
if (e.declaredType.hasNonScalar()) {
return true;
}
}
return false;
}
static ImmutableSet<EnumType> union(
ImmutableSet<EnumType> s1, ImmutableSet<EnumType> s2) {
if (s1.isEmpty()) {
return s2;
}
if (s2.isEmpty() || s1.equals(s2)) {
return s1;
}
return Sets.union(s1, s2).immutableCopy();
}
// We normalize the type so that it doesn't contain both enum {T1} and T1.
static ImmutableSet<EnumType> normalizeForJoin(
ImmutableSet<EnumType> newEnums, JSType joinWithoutEnums) {
boolean recreateEnums = false;
for (EnumType e : newEnums) {
if (e.declaredType.isSubtypeOf(joinWithoutEnums, SubtypeCache.create())) {
recreateEnums = true;
break;
}
}
if (!recreateEnums) {
return newEnums;
}
ImmutableSet.Builder<EnumType> builder = ImmutableSet.builder();
for (EnumType e : newEnums) {
if (!e.declaredType.isSubtypeOf(joinWithoutEnums, SubtypeCache.create())) {
builder.add(e);
}
}
return builder.build();
}
static boolean areSubtypes(JSType t1, JSType t2, SubtypeCache subSuperMap) {
ImmutableSet<EnumType> s1 = t1.getEnums();
if (s1 == null) {
return true;
}
ImmutableSet<EnumType> s2 = t2.getEnums();
for (EnumType e : s1) {
if (s2 != null && s2.contains(e)) {
continue;
}
if (!e.declaredType.isSubtypeOf(t2, subSuperMap)) {
return false;
}
}
return true;
}
@Override
public Collection<JSType> getSubtypesWithProperty(QualifiedName qname) {
return declaredType.getSubtypesWithProperty(qname);
}
}