-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
NominalType.java
555 lines (499 loc) · 18 KB
/
NominalType.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
/*
* Copyright 2013 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.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Multimap;
import com.google.javascript.rhino.Node;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Pattern;
/**
* @author blickly@google.com (Ben Lickly)
* @author dimvar@google.com (Dimitris Vardoulakis)
*/
public final class NominalType {
// In the case of a generic type (rawType.typeParameters non-empty) either:
// a) typeMap is empty, this is an uninstantiated generic type (Foo<T>), or
// b) typeMap's keys exactly correspond to the type parameters of rawType;
// this represents a completely instantiated generic type (Foo<number>).
private final ImmutableMap<String, JSType> typeMap;
private final RawNominalType rawType;
private static final Pattern NUMERIC_PATTERN = Pattern.compile("\\d+");
NominalType(ImmutableMap<String, JSType> typeMap, RawNominalType rawType) {
Preconditions.checkState(typeMap.isEmpty()
|| typeMap.keySet().containsAll(rawType.getTypeParameters())
&& rawType.getTypeParameters().containsAll(typeMap.keySet()));
this.typeMap = typeMap;
this.rawType = rawType;
}
// This should only be called during GlobalTypeInfo. All other calling contexts
// expect fully-instantiated types for properties defined on types, etc., but by accessing
// the raw nominal type directly they will get the uninstantiated generic types instead.
public RawNominalType getRawNominalType() {
// If the raw nominal type is finalized, then we are not in GlobalTypeInfo any more.
Preconditions.checkState(!this.rawType.isFinalized());
return this.rawType;
}
public JSType getInstanceAsJSType() {
return (this.rawType.isGeneric() && !typeMap.isEmpty())
? JSType.fromObjectType(ObjectType.fromNominalType(this))
: this.rawType.getInstanceAsJSType();
}
ObjectKind getObjectKind() {
return this.rawType.getObjectKind();
}
Map<String, JSType> getTypeMap() {
return typeMap;
}
JSType getIndexType() {
if (isIObject()) {
return this.typeMap.get(this.rawType.getTypeParameters().get(0));
}
// This type is a subtype of all indexed types it inherits from,
// and we use contravariance for the key of the index operation,
// so we join here.
JSType result = JSType.BOTTOM;
for (NominalType interf : getInstantiatedIObjectInterfaces()) {
JSType tmp = interf.getIndexType();
if (tmp != null) {
result = JSType.join(result, tmp);
}
}
return result.isBottom() ? null : result;
}
JSType getIndexedType() {
if (isIObject()) {
return this.typeMap.get(this.rawType.getTypeParameters().get(1));
}
// This type is a subtype of all indexed types it inherits from,
// and we use covariance for the value of the index operation,
// so we meet here.
JSType result = JSType.TOP;
// We need this because the index type may explicitly be TOP.
boolean foundIObject = false;
for (NominalType interf : getInstantiatedIObjectInterfaces()) {
JSType tmp = interf.getIndexedType();
if (tmp != null) {
foundIObject = true;
result = JSType.meet(result, tmp);
}
}
return foundIObject ? result : null;
}
boolean inheritsFromIObjectReflexive() {
return this.rawType.inheritsFromIObjectReflexive();
}
boolean isClassy() {
return !isFunction() && !isBuiltinObject();
}
boolean isFunction() {
return this.rawType.isBuiltinWithName("Function");
}
public boolean isBuiltinObject() {
return this.rawType.isBuiltinWithName("Object");
}
boolean isIObject() {
return this.rawType.isBuiltinWithName("IObject");
}
public boolean isStruct() {
return this.rawType.isStruct();
}
public boolean isDict() {
return this.rawType.isDict();
}
public boolean isGeneric() {
return this.rawType.isGeneric();
}
public boolean isUninstantiatedGenericType() {
return this.rawType.isGeneric() && typeMap.isEmpty();
}
public Node getDefSite() {
return this.rawType.getDefSite();
}
public FunctionType getConstructorFunction() {
return this.rawType.getConstructorFunction();
}
NominalType instantiateGenerics(List<JSType> types) {
ImmutableList<String> typeParams = this.rawType.getTypeParameters();
Preconditions.checkState(types.size() == typeParams.size());
Map<String, JSType> typeMap = new LinkedHashMap<>();
for (int i = 0; i < typeParams.size(); i++) {
typeMap.put(typeParams.get(i), types.get(i));
}
return instantiateGenerics(typeMap);
}
NominalType instantiateGenerics(Map<String, JSType> newTypeMap) {
if (newTypeMap.isEmpty()) {
return this;
}
if (!this.rawType.isGeneric()) {
return this.rawType.getAsNominalType();
}
ImmutableMap.Builder<String, JSType> builder = ImmutableMap.builder();
ImmutableMap<String, JSType> resultMap;
if (!typeMap.isEmpty()) {
for (String oldKey : typeMap.keySet()) {
builder.put(oldKey, typeMap.get(oldKey).substituteGenerics(newTypeMap));
}
resultMap = builder.build();
} else {
ImmutableList<String> typeParams = this.rawType.getTypeParameters();
for (String newKey : typeParams) {
if (newTypeMap.containsKey(newKey)) {
builder.put(newKey, newTypeMap.get(newKey));
}
}
resultMap = builder.build();
if (resultMap.isEmpty()) {
return this;
}
// This works around a bug in FunctionType, because we can't know where
// FunctionType#receiverType is coming from.
// If the condition is true, receiverType comes from a method declaration,
// and we should not create a new type here.
if (resultMap.size() < typeParams.size()) {
return this;
}
}
return new NominalType(resultMap, this.rawType);
}
public String getName() {
return this.rawType.name;
}
// Only used for keys in GlobalTypeInfo
public RawNominalType getId() {
return this.rawType;
}
public boolean isClass() {
return this.rawType.isClass();
}
public boolean isInterface() {
return this.rawType.isInterface();
}
boolean isStructuralInterface() {
return this.rawType.isStructuralInterface();
}
public boolean isFinalized() {
return this.rawType.isFinalized();
}
boolean hasAncestorClass(RawNominalType ancestor) {
return this.rawType.hasAncestorClass(ancestor);
}
boolean hasAncestorInterface(RawNominalType ancestor) {
return this.rawType.hasAncestorInterface(ancestor);
}
public ImmutableSet<String> getAllPropsOfInterface() {
return this.rawType.getAllPropsOfInterface();
}
public ImmutableSet<String> getAllPropsOfClass() {
return this.rawType.getAllPropsOfClass();
}
public NominalType getInstantiatedSuperclass() {
Preconditions.checkState(this.rawType.isFinalized());
if (this.rawType.getSuperClass() == null) {
return null;
}
return this.rawType.getSuperClass().instantiateGenerics(typeMap);
}
public JSType getPrototypePropertyOfCtor() {
Preconditions.checkState(this.rawType.isFinalized());
return this.rawType.getCtorPropDeclaredType("prototype")
.substituteGenerics(typeMap);
}
// We require finalization for the interfaces here because the inheritance
// chain of each type may not be correct until after the type is finalized.
public ImmutableSet<NominalType> getInstantiatedInterfaces() {
Preconditions.checkState(this.rawType.isFinalized());
ImmutableSet.Builder<NominalType> result = ImmutableSet.builder();
for (NominalType interf : this.rawType.getInterfaces()) {
result.add(interf.instantiateGenerics(typeMap));
}
return result.build();
}
// The main difference from getInstantiatedInterfaces is that this method
// can be used on non-finalized types.
private ImmutableSet<NominalType> getInstantiatedIObjectInterfaces() {
ImmutableSet.Builder<NominalType> result = ImmutableSet.builder();
for (NominalType interf : this.rawType.getInterfaces()) {
if (interf.inheritsFromIObjectReflexive()) {
result.add(interf.instantiateGenerics(typeMap));
}
}
return result.build();
}
Property getProp(String pname) {
if (this.rawType.name.equals("Array")
&& NUMERIC_PATTERN.matcher(pname).matches()) {
if (typeMap.isEmpty()) {
return Property.make(JSType.UNKNOWN, null);
}
Preconditions.checkState(typeMap.size() == 1);
JSType elmType = Iterables.getOnlyElement(typeMap.values());
return Property.make(elmType, null);
}
Property p = this.rawType.getProp(pname);
// TODO(aravindpg): Also look for getters and setters specially (in RawNominalType::protoProps),
// but avoid putting them in the hot path of getProp.
return p == null ? null : p.substituteGenerics(typeMap);
}
public JSType getPropDeclaredType(String pname) {
JSType type = this.rawType.getInstancePropDeclaredType(pname);
if (type == null) {
return null;
}
return type.substituteGenerics(typeMap);
}
Property getOwnProp(String pname) {
Property p = this.rawType.getOwnProp(pname);
return p == null ? null : p.substituteGenerics(typeMap);
}
public boolean hasConstantProp(String pname) {
Property p = this.rawType.getProp(pname);
return p != null && p.isConstant();
}
boolean isSubtypeOf(NominalType other, SubtypeCache subSuperMap) {
return isNominalSubtypeOf(other)
|| other.isStructuralInterface() && isStructuralSubtypeOf(other, subSuperMap);
}
private boolean isStructuralSubtypeOf(NominalType other, SubtypeCache subSuperMap) {
Preconditions.checkArgument(other.isStructuralInterface());
for (String pname : other.getAllPropsOfInterface()) {
Property prop2 = other.getProp(pname);
Property prop1 = this.getProp(pname);
if (prop2.isOptional()) {
if (prop1 != null
&& !prop1.getType().isSubtypeOf(prop2.getType(), subSuperMap)) {
return false;
}
} else if (prop1 == null || prop1.isOptional()
|| !prop1.getType().isSubtypeOf(prop2.getType(), subSuperMap)) {
return false;
}
}
return true;
}
boolean isNominalSubtypeOf(NominalType other) {
RawNominalType thisRaw = this.rawType;
if (thisRaw == other.rawType) {
return areTypeMapsCompatible(other);
}
if (other.isBuiltinObject()) {
return true;
}
if (other.isInterface()) {
// If thisRaw is not finalized, thisRaw.interfaces may be null.
for (NominalType i : thisRaw.getInterfaces()) {
if (i.instantiateGenerics(this.typeMap).isNominalSubtypeOf(other)) {
return true;
}
}
}
// Note that other can still be an interface here (implemented by a superclass)
return isClass() && thisRaw.getSuperClass() != null
&& thisRaw.getSuperClass().instantiateGenerics(this.typeMap).isNominalSubtypeOf(other);
}
private boolean areTypeMapsCompatible(NominalType other) {
Preconditions.checkState(this.rawType.equals(other.rawType));
if (this.typeMap.isEmpty()) {
return other.instantiationIsUnknownOrIdentity();
}
if (other.typeMap.isEmpty()) {
return instantiationIsUnknownOrIdentity();
}
for (String typeVar : this.rawType.getTypeParameters()) {
Preconditions.checkState(this.typeMap.containsKey(typeVar),
"Type variable %s not in the domain: %s",
typeVar, this.typeMap.keySet());
Preconditions.checkState(other.typeMap.containsKey(typeVar),
"Other (%s) doesn't contain mapping (%s->%s) from this (%s)",
other, typeVar, this.typeMap.get(typeVar), this);
JSType thisType = this.typeMap.get(typeVar);
JSType otherType = other.typeMap.get(typeVar);
if (!thisType.isSubtypeOf(otherType)) {
return false;
}
}
return true;
}
/**
* Unify the two types symmetrically, given that we have already instantiated
* the type variables of interest in {@code nt1} and {@code nt2}, treating
* JSType.UNKNOWN as a "hole" to be filled.
* @return The unified type, or null if unification fails
*/
static NominalType unifyUnknowns(NominalType nt1, NominalType nt2) {
if (!nt1.rawType.equals(nt2.rawType)) {
return null;
}
Map<String, JSType> m1 = nt1.typeMap;
Map<String, JSType> m2 = nt2.typeMap;
if (m1.isEmpty() && m2.isEmpty()) {
return nt1;
} else if (m1.isEmpty() || m2.isEmpty()) {
return null;
}
ImmutableMap.Builder<String, JSType> builder = ImmutableMap.builder();
for (Map.Entry<String, JSType> entry : m1.entrySet()) {
String typeVar = entry.getKey();
JSType t1 = entry.getValue();
JSType t2 = m2.get(typeVar);
if (t1.isUnknown()) {
builder.put(typeVar, t2);
} else if (t2.isUnknown()) {
builder.put(typeVar, t1);
} else {
JSType newType = JSType.unifyUnknowns(t1, t2);
if (newType == null) {
return null;
}
builder.put(typeVar, newType);
}
}
return new NominalType(builder.build(), nt1.rawType);
}
private boolean instantiationIsUnknownOrIdentity() {
if (this.typeMap.isEmpty()) {
return true;
}
for (String typeVar : this.rawType.getTypeParameters()) {
Preconditions.checkState(this.typeMap.containsKey(typeVar),
"Type variable %s not in the domain: %s",
typeVar, this.typeMap.keySet());
JSType t = this.typeMap.get(typeVar);
if (!t.isUnknown() && !t.equals(JSType.fromTypeVar(typeVar))) {
return false;
}
}
return true;
}
// A special-case of join
static NominalType pickSuperclass(NominalType c1, NominalType c2) {
if (c1 == null || c2 == null) {
return null;
}
if (c1.isNominalSubtypeOf(c2)) {
return c2;
}
return c2.isNominalSubtypeOf(c1) ? c1 : null;
}
// A special-case of meet
static NominalType pickSubclass(NominalType c1, NominalType c2) {
if (c1 == null) {
return c2;
}
if (c2 == null) {
return c1;
}
if (c1.isNominalSubtypeOf(c2)) {
return c1;
}
return c2.isNominalSubtypeOf(c1) ? c2 : null;
}
boolean unifyWithSubtype(NominalType other, List<String> typeParameters,
Multimap<String, JSType> typeMultimap, SubtypeCache subSuperMap) {
other = other.findMatchingAncestorWith(this);
if (other == null) {
return false;
}
if (!isGeneric()) {
// Non-generic nominal types don't contribute to the unification.
return true;
}
// Most of the time, both nominal types are already instantiated when
// unifyWith is called. Rarely, when we call a polymorphic function from the
// body of a method of a polymorphic class, then other.typeMap is
// empty. For now, don't do anything fancy in that case.
Preconditions.checkState(!typeMap.isEmpty());
if (other.typeMap.isEmpty()) {
return true;
}
boolean hasUnified = true;
for (String typeParam : this.rawType.getTypeParameters()) {
JSType fromOtherMap = other.typeMap.get(typeParam);
Preconditions.checkNotNull(fromOtherMap,
"Type variable %s not found in map %s",
typeParam, other.typeMap);
hasUnified = hasUnified && this.typeMap.get(typeParam)
.unifyWithSubtype(fromOtherMap, typeParameters, typeMultimap, subSuperMap);
}
return hasUnified;
}
// Returns a type with the same raw type as other, but possibly different type maps.
private NominalType findMatchingAncestorWith(NominalType other) {
RawNominalType thisRaw = this.rawType;
if (thisRaw == other.rawType) {
return this;
}
if (other.isInterface()) {
for (NominalType i : thisRaw.getInterfaces()) {
NominalType nt = i.instantiateGenerics(this.typeMap).findMatchingAncestorWith(other);
if (nt != null) {
return nt;
}
}
}
// Note that other can still be an interface here (implemented by a superclass)
if (isClass() && thisRaw.getSuperClass() != null) {
return thisRaw.getSuperClass().instantiateGenerics(this.typeMap)
.findMatchingAncestorWith(other);
}
return null;
}
@Override
public String toString() {
return appendTo(new StringBuilder()).toString();
}
StringBuilder appendTo(StringBuilder builder) {
if (this.typeMap.isEmpty()) {
return this.rawType.appendTo(builder);
}
builder.append(this.rawType.name);
ImmutableList<String> typeParams = this.rawType.getTypeParameters();
Preconditions.checkState(this.typeMap.keySet().containsAll(typeParams));
boolean firstIteration = true;
builder.append('<');
for (String typeParam : typeParams) {
if (firstIteration) {
firstIteration = false;
} else {
builder.append(',');
}
JSType concrete = this.typeMap.get(typeParam);
Preconditions.checkNotNull(concrete).appendTo(builder);
}
builder.append('>');
return builder;
}
@Override
public int hashCode() {
return Objects.hash(typeMap, this.rawType);
}
@Override
public boolean equals(Object other) {
if (other == null) {
return false;
}
Preconditions.checkState(other instanceof NominalType);
NominalType o = (NominalType) other;
return this.rawType.equals(o.rawType) && Objects.equals(typeMap, o.typeMap);
}
}