-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
NominalType.java
752 lines (673 loc) · 25.4 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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
/*
* 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 static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
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.io.Serializable;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.regex.Pattern;
/**
* @author blickly@google.com (Ben Lickly)
* @author dimvar@google.com (Dimitris Vardoulakis)
*/
public final class NominalType implements Serializable {
// 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) {
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 frozen, then we are not in GlobalTypeInfo any more.
checkState(!this.rawType.isFrozen());
return this.rawType;
}
// This is used for DisambiguateProperties. Do not call during NewTypeInference.
// See note for getRawNominalType.
public RawNominalType getRawNominalTypeAfterTypeChecking() {
return this.rawType;
}
// NOTE(dimvar): we need this to get access to the static properties of the class.
// It'd be good if these properties were on the type returned by getConstructorFunction,
// but there are some circularity issues when we're computing the namespace types.
// Maybe revisit in the future to improve this.
public JSType getNamespaceType() {
return this.rawType.toJSType();
}
public JSType getInstanceAsJSType() {
return (this.rawType.isGeneric() && !typeMap.isEmpty())
? JSType.fromObjectType(ObjectType.fromNominalType(this))
: this.rawType.getInstanceAsJSType();
}
ObjectType getInstanceAsObjectType() {
return (this.rawType.isGeneric() && !typeMap.isEmpty())
? ObjectType.fromNominalType(this)
: this.rawType.getInstanceAsJSType().getObjTypeIfSingletonObj();
}
JSTypes getCommonTypes() {
return this.rawType.getCommonTypes();
}
ObjectKind getObjectKind() {
return this.rawType.getObjectKind();
}
Map<String, JSType> getTypeMap() {
return typeMap;
}
ImmutableList<String> getTypeParameters() {
return this.rawType.getTypeParameters();
}
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 = getCommonTypes().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 = getCommonTypes().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() && !isLiteralObject();
}
public boolean isFunction() {
return this.rawType.isBuiltinWithName("Function");
}
public boolean isBuiltinObject() {
return this.rawType.isBuiltinObject();
}
public boolean isLiteralObject() {
return this.rawType.isBuiltinWithName(JSTypes.OBJLIT_CLASS_NAME);
}
boolean isIObject() {
return this.rawType.isBuiltinWithName("IObject");
}
boolean isIArrayLike() {
return this.rawType.isBuiltinWithName("IArrayLike");
}
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 JSType getPrototypeObject() {
return this.rawType.getPrototypeObject();
}
public FunctionType getConstructorFunction() {
if (this.typeMap.isEmpty()) {
return this.rawType.getConstructorFunction();
}
return this.rawType.getConstructorFunction().instantiateGenerics(this.typeMap);
}
/**
* Substitute the free type variables in this type using the provided type map.
* The most common case when this happens is when a nominal type has already been "instantiated"
* to type variables, and now we want to substitute concrete types for these type variables.
* For example, in the program below, Array's T is instantiated to U in the type of f,
* and when we call f, we substitute boolean for U.
*/
// Written as line comment to enable use of jsdoc
// /**
// * @template U
// * @param {!Array<U>} x
// */
// function f(x) { return x[0]; }
// f([true, false]);
NominalType substituteGenerics(Map<String, JSType> newTypeMap) {
if (!isGeneric()) {
return this.rawType.getAsNominalType();
}
// NOTE(dimvar): in rare cases, because of the way we represent types, we may end up calling
// substituteGenerics on a nominal type that has an empty type map, which is counter-intuitive.
// Might be worth it at some point to identify all those cases and make sure that types are
// instantiated to identity, rather than having an empty type map. Not super important though.
if (this.typeMap.isEmpty()) {
return instantiateGenerics(newTypeMap);
}
if (newTypeMap.isEmpty()) {
return this;
}
ImmutableMap.Builder<String, JSType> builder = ImmutableMap.builder();
for (String oldKey : this.typeMap.keySet()) {
builder.put(oldKey, this.typeMap.get(oldKey).substituteGenerics(newTypeMap));
}
return new NominalType(builder.build(), this.rawType);
}
NominalType instantiateGenerics(Map<String, JSType> newTypeMap) {
if (newTypeMap.isEmpty()) {
return this;
}
if (!this.rawType.isGeneric()) {
return this.rawType.getAsNominalType();
}
Preconditions.checkState(this.typeMap.isEmpty(),
"Expected empty typemap, found: %s", this.typeMap);
ImmutableMap.Builder<String, JSType> builder = ImmutableMap.builder();
ImmutableMap<String, JSType> resultMap;
ImmutableList<String> typeParams = 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);
}
NominalType instantiateGenerics(List<JSType> types) {
ImmutableList<String> typeParams = this.rawType.getTypeParameters();
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 instantiateGenericsWithUnknown() {
NominalType thisWithoutTypemap = this.rawType.getAsNominalType();
return thisWithoutTypemap.instantiateGenerics(getCommonTypes().MAP_TO_UNKNOWN);
}
NominalType instantiateGenericsWithIdentity() {
checkState(isUninstantiatedGenericType());
Map<String, JSType> m = new LinkedHashMap<>();
for (String typeParam : this.getTypeParameters()) {
m.put(typeParam, JSType.fromTypeVar(this.getCommonTypes(), typeParam));
}
return instantiateGenerics(m);
}
public String getName() {
return this.rawType.name;
}
// Only used for keys in GlobalTypeInfo
public RawNominalType getId() {
return this.rawType;
}
Set<RawNominalType> getSubtypes() {
return this.rawType.getSubtypes();
}
public boolean isClass() {
return this.rawType.isClass();
}
public boolean isAbstractClass() {
return this.rawType.isAbstractClass();
}
public boolean isInterface() {
return this.rawType.isInterface();
}
boolean isStructuralInterface() {
return this.rawType.isStructuralInterface();
}
public boolean isFrozen() {
return this.rawType.isFrozen();
}
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 Set<String> getAllOwnClassProps() {
return this.rawType.getAllOwnClassProps();
}
public NominalType getInstantiatedSuperclass() {
checkState(this.rawType.isFrozen());
if (this.rawType.getSuperClass() == null) {
return null;
}
return this.rawType.getSuperClass().substituteGenerics(typeMap);
}
public JSType getPrototypePropertyOfCtor() {
checkState(this.rawType.isFrozen());
return this.rawType.getCtorPropDeclaredType("prototype");
}
// We require a frozen type for the interfaces here because the inheritance
// chain of each type may not be correct until after the type is frozen.
public ImmutableSet<NominalType> getInstantiatedInterfaces() {
checkState(this.rawType.isFrozen());
ImmutableSet.Builder<NominalType> result = ImmutableSet.builder();
for (NominalType interf : this.rawType.getInterfaces()) {
result.add(interf.substituteGenerics(typeMap));
}
return result.build();
}
// The main difference from getInstantiatedInterfaces is that this method
// can be used on non-frozen types.
private ImmutableSet<NominalType> getInstantiatedIObjectInterfaces() {
ImmutableSet.Builder<NominalType> result = ImmutableSet.builder();
for (NominalType interf : this.rawType.getInterfaces()) {
if (interf.inheritsFromIObjectReflexive()) {
result.add(interf.substituteGenerics(typeMap));
}
}
return result.build();
}
NominalType getTopDefiningInterface(String pname) {
Preconditions.checkState(isInterface(), "Expected interface, found: %s", this);
NominalType result = null;
if (getOwnProp(pname) != null) {
result = this;
}
for (NominalType nt : this.getInstantiatedInterfaces()) {
if (nt.getOwnProp(pname) != null) {
result = nt.getTopDefiningInterface(pname);
}
}
return result;
}
Property getProp(String pname) {
if (this.rawType.isBuiltinWithName("Array")
&& NUMERIC_PATTERN.matcher(pname).matches()) {
if (typeMap.isEmpty()) {
return Property.make(getCommonTypes().UNKNOWN, null);
}
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 mayHaveProp(String pname) {
return this.rawType.mayHaveProp(pname);
}
public boolean hasAbstractMethod(String pname) {
return this.rawType.hasAbstractMethod(pname);
}
boolean isSubtypeOf(NominalType other, SubtypeCache subSuperMap) {
return isNominalSubtypeOf(other)
|| other.isStructuralInterface() && isStructuralSubtypeOf(other, subSuperMap);
}
private boolean isStructuralSubtypeOf(NominalType other, SubtypeCache subSuperMap) {
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;
}
// Checks for subtyping without taking generics into account
boolean isRawSubtypeOf(NominalType other) {
return this.rawType.isSubtypeOf(other.rawType);
}
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 frozen, thisRaw.interfaces may be null.
for (NominalType i : thisRaw.getInterfaces()) {
if (i.substituteGenerics(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().substituteGenerics(this.typeMap).isNominalSubtypeOf(other);
}
boolean isIObjectSubtypeOf(NominalType other) {
checkState(this.inheritsFromIObjectReflexive() && other.inheritsFromIObjectReflexive());
// Contravariance for the index type and covariance for the indexed type.
return other.getIndexType().isSubtypeOf(this.getIndexType())
&& this.getIndexedType().isSubtypeOf(other.getIndexedType());
}
private boolean areTypeMapsCompatible(NominalType other) {
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);
JSTypes commonTypes = getCommonTypes();
if (commonTypes.bivariantArrayGenerics && this.rawType.isBuiltinWithName("Array")) {
thisType = thisType.removeType(commonTypes.NULL_OR_UNDEFINED);
otherType = otherType.removeType(commonTypes.NULL_OR_UNDEFINED);
if (!thisType.isSubtypeOf(otherType) && !otherType.isSubtypeOf(thisType)) {
return false;
}
} else 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(getCommonTypes(), typeVar))) {
return false;
}
}
return true;
}
private static ImmutableMap<String, JSType> joinTypeMaps(NominalType nt1, NominalType nt2) {
checkState(nt1.rawType.equals(nt2.rawType));
ImmutableMap.Builder<String, JSType> builder = ImmutableMap.builder();
if (nt1.isIObject()) {
// Special case IObject, whose first type parameter is contravariant.
String indexTypevar = nt1.rawType.getTypeParameters().get(0);
builder.put(indexTypevar, JSType.meet(nt1.getIndexType(), nt2.getIndexType()));
String indexedTypevar = nt1.rawType.getTypeParameters().get(1);
builder.put(indexedTypevar, JSType.join(nt1.getIndexedType(), nt2.getIndexedType()));
return builder.build();
}
if (nt1.typeMap.isEmpty() || nt2.typeMap.isEmpty()) {
return ImmutableMap.of();
}
for (String typevar : nt1.typeMap.keySet()) {
builder.put(typevar, JSType.join(nt1.typeMap.get(typevar), nt2.typeMap.get(typevar)));
}
return builder.build();
}
// A special-case of join.
static NominalType join(NominalType c1, NominalType c2) {
if (c1 == null || c2 == null) {
return null;
}
if (c1.isNominalSubtypeOf(c2)) {
return c2;
}
if (c2.isNominalSubtypeOf(c1)) {
return c1;
}
if (c1.rawType.equals(c2.rawType)) {
return c1.isGeneric() ? new NominalType(joinTypeMaps(c1, c2), c1.rawType) : c1;
}
// If c1.isRawSubtypeOf(c2) but not c1.isNominalSubtypeOf(c2), we would want to change
// joinTypeMaps to handle type maps with different domains. Basically, we want to go up
// c1's inheritance chain and get instantiated ancestors until we reach the ancestor with the
// same raw type as c2, and then join.
// Putting the preconditions check in order to get notified if we ever need to handle this.
checkState(!c1.isRawSubtypeOf(c2) && !c2.isRawSubtypeOf(c1));
return 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 this.typeMap and/or other.typeMap
// can be empty. For now, don't do anything fancy in that case.
if (this.typeMap.isEmpty() || 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.substituteGenerics(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().substituteGenerics(this.typeMap)
.findMatchingAncestorWith(other);
}
return null;
}
boolean isPropDefinedOnSubtype(QualifiedName pname) {
checkArgument(pname.isIdentifier());
return this.rawType.isPropDefinedOnSubtype(pname.getLeftmostName());
}
Set<JSType> getSubtypesWithProperty(QualifiedName pname) {
checkArgument(pname.isIdentifier());
return this.rawType.getSubtypesWithProperty(pname.getLeftmostName());
}
static boolean equalRawTypes(NominalType n1, NominalType n2) {
return n1.rawType.equals(n2.rawType);
}
@Override
public String toString() {
return appendTo(new StringBuilder(), ToStringContext.TO_STRING).toString();
}
StringBuilder appendTo(StringBuilder builder, ToStringContext ctx) {
if (ctx.forAnnotation()) {
builder.append("!");
}
if (this.typeMap.isEmpty()) {
return this.rawType.appendTo(builder);
}
builder.append(this.rawType.name);
ImmutableList<String> typeParams = this.rawType.getTypeParameters();
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);
checkNotNull(concrete).appendTo(builder, ctx);
}
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;
}
if (this == other) {
return true;
}
checkState(other instanceof NominalType);
NominalType o = (NominalType) other;
return this.rawType.equals(o.rawType) && Objects.equals(typeMap, o.typeMap);
}
}