-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
UnionType.java
789 lines (727 loc) · 24.7 KB
/
UnionType.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
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
/*
*
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (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.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Rhino code, released
* May 6, 1999.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1997-1999
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Bob Jervis
* Google Inc.
*
* Alternatively, the contents of this file may be used under the terms of
* the GNU General Public License Version 2 or later (the "GPL"), in which
* case the provisions of the GPL are applicable instead of those above. If
* you wish to allow use of your version of this file only under the terms of
* the GPL and not to allow others to use your version of this file under the
* MPL, indicate your decision by deleting the provisions above and replacing
* them with the notice and other provisions required by the GPL. If you do
* not delete the provisions above, a recipient may use your version of this
* file under either the MPL or the GPL.
*
* ***** END LICENSE BLOCK ***** */
package com.google.javascript.rhino.jstype;
import static com.google.javascript.rhino.jstype.TernaryValue.UNKNOWN;
import com.google.common.base.Joiner;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableList;
import com.google.javascript.rhino.ErrorReporter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;
/**
* The {@code UnionType} implements a common JavaScript idiom in which the
* code is specifically designed to work with multiple input types. Because
* JavaScript always knows the run-time type of an object value, this is safer
* than a C union.<p>
*
* For instance, values of the union type {@code (String,boolean)} can be of
* type {@code String} or of type {@code boolean}. The commutativity of the
* statement is captured by making {@code (String,boolean)} and
* {@code (boolean,String)} equal.<p>
*
*
* The implementation of this class prevents the creation of nested
* unions.<p>
*/
public class UnionType extends JSType {
private static final long serialVersionUID = 1L;
// NOTE: to avoid allocating iterators, all the loops below iterate over alternates by index
// instead of using the for-each loop idiom.
// alternates without merging structural interfaces and their subtypes
ImmutableList<JSType> alternatesWithoutStucturalTyping;
// alternates under structural typing
ImmutableList<JSType> alternates;
private int hashcode;
/**
* Creates a union type.
*
* @param alternatesWithoutStructuralTyping the alternates of the union without structural typing
* subtype
*/
UnionType(JSTypeRegistry registry, ImmutableList<JSType> alternatesWithoutStructuralTyping) {
super(registry);
this.alternatesWithoutStucturalTyping = alternatesWithoutStructuralTyping;
UnionTypeBuilder builder = new UnionTypeBuilder(registry);
for (JSType alternate : alternatesWithoutStructuralTyping) {
builder.addAlternate(alternate, true);
}
this.alternates = builder.getAlternates();
this.hashcode = this.alternatesWithoutStucturalTyping.hashCode();
}
/**
* Gets the alternate types of this union type.
* @return The alternate types of this union type. The returned set is
* immutable.
*/
public Collection<JSType> getAlternates() {
return getAlternatesList();
}
/**
* Gets the alternate types of this union type.
*
* @return The alternate types of this union type. The returned list is immutable.
*/
ImmutableList<JSType> getAlternatesList() {
for (int i = 0; i < alternatesWithoutStucturalTyping.size(); i++) {
JSType t = alternatesWithoutStucturalTyping.get(i);
if (t.isUnionType()) {
rebuildAlternates();
break;
}
}
return alternates;
}
/**
* Gets the alternate types of this union type, including structural interfaces
* and implicit implementations as are distinct alternates.
* @return The alternate types of this union type. The returned set is
* immutable.
*/
public Collection<JSType> getAlternatesWithoutStructuralTyping() {
return getAlternatesWithoutStructuralTypingList();
}
/**
* Gets the alternate types of this union type, including structural interfaces and implicit
* implementations as are distinct alternates.
*
* @return The alternate types of this union type. The returned set is immutable.
*/
ImmutableList<JSType> getAlternatesWithoutStructuralTypingList() {
for (int i = 0; i < alternatesWithoutStucturalTyping.size(); i++) {
JSType t = alternatesWithoutStucturalTyping.get(i);
if (t.isUnionType()) {
rebuildAlternates();
break;
}
}
return alternatesWithoutStucturalTyping;
}
/**
* Use UnionTypeBuilder to rebuild the list of alternates and hashcode
* of the current UnionType.
*/
private void rebuildAlternates() {
UnionTypeBuilder builder = new UnionTypeBuilder(registry);
for (JSType alternate : alternatesWithoutStucturalTyping) {
builder.addAlternate(alternate);
}
alternatesWithoutStucturalTyping = builder.getAlternates();
builder = new UnionTypeBuilder(registry);
for (JSType alternate : alternatesWithoutStucturalTyping) {
builder.addAlternate(alternate, true);
}
alternates = builder.getAlternates();
hashcode = alternatesWithoutStucturalTyping.hashCode();
}
/**
* This predicate is used to test whether a given type can appear in a
* numeric context, such as an operand of a multiply operator.
*
* @return true if the type can appear in a numeric context.
*/
@Override
public boolean matchesNumberContext() {
// TODO(user): Reverse this logic to make it correct instead of generous.
for (JSType t : alternatesWithoutStucturalTyping) {
if (t.matchesNumberContext()) {
return true;
}
}
return false;
}
/**
* This predicate is used to test whether a given type can appear in a
* {@code String} context, such as an operand of a string concat ({@code +})
* operator.<p>
*
* All types have at least the potential for converting to {@code String}.
* When we add externally defined types, such as a browser OM, we may choose
* to add types that do not automatically convert to {@code String}.
*
* @return {@code true} if not {@link VoidType}
*/
@Override
public boolean matchesStringContext() {
// TODO(user): Reverse this logic to make it correct instead of generous.
for (JSType t : alternatesWithoutStucturalTyping) {
if (t.matchesStringContext()) {
return true;
}
}
return false;
}
/**
* This predicate is used to test whether a given type can appear in a {@code Symbol} context
*
* @return {@code true} if not it maybe a symbol or Symbol object
*/
@Override
public boolean matchesSymbolContext() {
for (JSType t : alternatesWithoutStucturalTyping) {
if (t.matchesSymbolContext()) {
return true;
}
}
return false;
}
/**
* This predicate is used to test whether a given type can appear in an
* {@code Object} context, such as the expression in a {@code with}
* statement.<p>
*
* Most types we will encounter, except notably {@code null}, have at least
* the potential for converting to {@code Object}. Host defined objects can
* get peculiar.<p>
*
* VOID type is included here because while it is not part of the JavaScript
* language, functions returning 'void' type can't be used as operands of
* any operator or statement.<p>
*
* @return {@code true} if the type is not {@link NullType} or
* {@link VoidType}
*/
@Override
public boolean matchesObjectContext() {
// TODO(user): Reverse this logic to make it correct instead of generous.
for (JSType t : alternatesWithoutStucturalTyping) {
if (t.matchesObjectContext()) {
return true;
}
}
return false;
}
@Override
public JSType findPropertyType(String propertyName) {
JSType propertyType = null;
for (JSType alternate : getAlternates()) {
// Filter out the null/undefined type.
if (alternate.isNullType() || alternate.isVoidType()) {
continue;
}
JSType altPropertyType = alternate.findPropertyType(propertyName);
if (altPropertyType == null) {
continue;
}
if (propertyType == null) {
propertyType = altPropertyType;
} else {
propertyType = propertyType.getLeastSupertype(altPropertyType);
}
}
return propertyType;
}
@Override
public boolean canBeCalled() {
for (int i = 0; i < alternatesWithoutStucturalTyping.size(); i++) {
JSType t = alternatesWithoutStucturalTyping.get(i);
if (!t.canBeCalled()) {
return false;
}
}
return true;
}
@Override
public JSType autobox() {
UnionTypeBuilder restricted = new UnionTypeBuilder(registry);
for (int i = 0; i < alternatesWithoutStucturalTyping.size(); i++) {
JSType t = alternatesWithoutStucturalTyping.get(i);
restricted.addAlternate(t.autobox());
}
return restricted.build();
}
@Override
public JSType restrictByNotNullOrUndefined() {
UnionTypeBuilder restricted = new UnionTypeBuilder(registry);
for (int i = 0; i < alternatesWithoutStucturalTyping.size(); i++) {
JSType t = alternatesWithoutStucturalTyping.get(i);
restricted.addAlternate(t.restrictByNotNullOrUndefined());
}
return restricted.build();
}
@Override
public TernaryValue testForEquality(JSType that) {
TernaryValue result = null;
for (int i = 0; i < alternatesWithoutStucturalTyping.size(); i++) {
JSType t = alternatesWithoutStucturalTyping.get(i);
TernaryValue test = t.testForEquality(that);
if (result == null) {
result = test;
} else if (!result.equals(test)) {
return UNKNOWN;
}
}
return result;
}
/**
* This predicate determines whether objects of this type can have the
* {@code null} value, and therefore can appear in contexts where
* {@code null} is expected.
*
* @return {@code true} for everything but {@code Number} and
* {@code Boolean} types.
*/
@Override
public boolean isNullable() {
for (int i = 0; i < alternatesWithoutStucturalTyping.size(); i++) {
JSType t = alternatesWithoutStucturalTyping.get(i);
if (t.isNullable()) {
return true;
}
}
return false;
}
/**
* Tests whether this type is voidable.
*/
@Override
public boolean isVoidable() {
for (int i = 0; i < alternatesWithoutStucturalTyping.size(); i++) {
JSType t = alternatesWithoutStucturalTyping.get(i);
if (t.isVoidable()) {
return true;
}
}
return false;
}
/**
* Tests whether this type explicitly allows undefined. (as opposed to ? or *)
*/
@Override
public boolean isExplicitlyVoidable() {
for (int i = 0; i < alternatesWithoutStucturalTyping.size(); i++) {
JSType t = alternatesWithoutStucturalTyping.get(i);
if (t.isExplicitlyVoidable()) {
return true;
}
}
return false;
}
@Override
public boolean isUnknownType() {
for (int i = 0; i < alternatesWithoutStucturalTyping.size(); i++) {
JSType t = alternatesWithoutStucturalTyping.get(i);
if (t.isUnknownType()) {
return true;
}
}
return false;
}
@Override
public boolean isStruct() {
List<JSType> alternates = getAlternatesList();
for (int i = 0; i < alternates.size(); i++) {
JSType typ = alternates.get(i);
if (typ.isStruct()) {
return true;
}
}
return false;
}
@Override
public boolean isDict() {
List<JSType> alternates = getAlternatesList();
for (int i = 0; i < alternates.size(); i++) {
JSType typ = alternates.get(i);
if (typ.isDict()) {
return true;
}
}
return false;
}
@Override
public JSType getLeastSupertype(JSType that) {
if (!that.isUnknownType() && !that.isUnionType()) {
for (int i = 0; i < alternatesWithoutStucturalTyping.size(); i++) {
JSType alternate = alternatesWithoutStucturalTyping.get(i);
if (!alternate.isUnknownType() && that.isSubtypeOf(alternate)) {
return this;
}
}
}
return JSType.getLeastSupertype(this, that);
}
JSType meet(JSType that) {
UnionTypeBuilder builder = new UnionTypeBuilder(registry);
for (int i = 0; i < alternatesWithoutStucturalTyping.size(); i++) {
JSType alternate = alternatesWithoutStucturalTyping.get(i);
if (alternate.isSubtypeOf(that)) {
builder.addAlternate(alternate);
}
}
if (that.isUnionType()) {
List<JSType> thoseAlternatesWithoutStucturalTyping =
that.toMaybeUnionType().alternatesWithoutStucturalTyping;
for (int i = 0; i < thoseAlternatesWithoutStucturalTyping.size(); i++) {
JSType otherAlternate = thoseAlternatesWithoutStucturalTyping.get(i);
if (otherAlternate.isSubtypeOf(this)) {
builder.addAlternate(otherAlternate);
}
}
} else if (that.isSubtypeOf(this)) {
builder.addAlternate(that);
}
JSType result = builder.build();
if (!result.isNoType()) {
return result;
} else if (this.isObject() && (that.isObject() && !that.isNoType())) {
return getNativeType(JSTypeNative.NO_OBJECT_TYPE);
} else {
return getNativeType(JSTypeNative.NO_TYPE);
}
}
/**
* Two union types are equal if, after flattening nested union types,
* they have the same number of alternates and all alternates are equal.
*/
boolean checkUnionEquivalenceHelper(
UnionType that, EquivalenceMethod eqMethod, EqCache eqCache) {
List<JSType> thatAlternates = that.getAlternatesWithoutStructuralTypingList();
if (eqMethod == EquivalenceMethod.IDENTITY
&& getAlternatesWithoutStructuralTyping().size() != thatAlternates.size()) {
return false;
}
for (int i = 0; i < thatAlternates.size(); i++) {
JSType thatAlternate = thatAlternates.get(i);
if (!hasAlternate(thatAlternate, eqMethod, eqCache)) {
return false;
}
}
return true;
}
private boolean hasAlternate(JSType type, EquivalenceMethod eqMethod,
EqCache eqCache) {
List<JSType> alternatesWithoutStructuralTyping = getAlternatesWithoutStructuralTypingList();
for (int i = 0; i < alternatesWithoutStructuralTyping.size(); i++) {
JSType alternate = alternatesWithoutStructuralTyping.get(i);
if (alternate.checkEquivalenceHelper(type, eqMethod, eqCache)) {
return true;
}
}
return false;
}
@Override
public HasPropertyKind getPropertyKind(String pname, boolean autobox) {
boolean found = false;
boolean always = true;
for (int i = 0; i < alternatesWithoutStucturalTyping.size(); i++) {
JSType alternate = alternatesWithoutStucturalTyping.get(i);
if (alternate.isNullType() || alternate.isVoidType()) {
continue;
}
switch (alternate.getPropertyKind(pname, autobox)) {
case KNOWN_PRESENT:
found = true;
break;
case ABSENT:
always = false;
break;
case MAYBE_PRESENT:
found = true;
always = false;
break;
}
if (found && !always) {
break;
}
}
return found
? (always ? HasPropertyKind.KNOWN_PRESENT : HasPropertyKind.MAYBE_PRESENT)
: HasPropertyKind.ABSENT;
}
@Override
int recursionUnsafeHashCode() {
return this.hashcode;
}
@Override
public UnionType toMaybeUnionType() {
return this;
}
@Override
public boolean isObject() {
for (int i = 0; i < alternatesWithoutStucturalTyping.size(); i++) {
JSType alternate = alternatesWithoutStucturalTyping.get(i);
if (!alternate.isObject()) {
return false;
}
}
return true;
}
/**
* A {@link UnionType} contains a given type (alternate) iff the member
* vector contains it.
*
* @param type The alternate which might be in this union.
*
* @return {@code true} if the alternate is in the union
*/
public boolean contains(JSType type) {
for (int i = 0; i < alternatesWithoutStucturalTyping.size(); i++) {
JSType alt = alternatesWithoutStucturalTyping.get(i);
if (alt.isEquivalentTo(type)) {
return true;
}
}
return false;
}
/**
* Returns a more restricted union type than {@code this} one, in which all
* subtypes of {@code type} have been removed.<p>
*
* Examples:
* <ul>
* <li>{@code (number,string)} restricted by {@code number} is
* {@code string}</li>
* <li>{@code (null, EvalError, URIError)} restricted by
* {@code Error} is {@code null}</li>
* </ul>
*
* @param type the supertype of the types to remove from this union type
*/
public JSType getRestrictedUnion(JSType type) {
UnionTypeBuilder restricted = new UnionTypeBuilder(registry);
for (int i = 0; i < alternatesWithoutStucturalTyping.size(); i++) {
JSType t = alternatesWithoutStucturalTyping.get(i);
// Keep all unknown/unresolved types.
if (t.isUnknownType() || t.isNoResolvedType() || !t.isSubtypeOf(type)) {
restricted.addAlternate(t);
}
}
return restricted.build();
}
@Override
StringBuilder appendTo(StringBuilder sb, boolean forAnnotations) {
sb.append("(");
// Sort types in character value order in order to get consistent results.
// This is important for deterministic behavior for testing.
SortedSet<String> sortedTypeNames = new TreeSet<>();
for (JSType jsType : alternatesWithoutStucturalTyping) {
sortedTypeNames.add(jsType.appendTo(new StringBuilder(), forAnnotations).toString());
}
Joiner.on('|').appendTo(sb, sortedTypeNames);
return sb.append(")");
}
@Override
public boolean isSubtype(JSType that) {
return isSubtype(that, ImplCache.create(), SubtypingMode.NORMAL);
}
@Override
protected boolean isSubtype(JSType that,
ImplCache implicitImplCache, SubtypingMode subtypingMode) {
// unknown
if (that.isUnknownType() || this.isUnknownType()) {
return true;
}
// all type
if (that.isAllType()) {
return true;
}
for (int i = 0; i < alternatesWithoutStucturalTyping.size(); i++) {
JSType element = alternatesWithoutStucturalTyping.get(i);
if (subtypingMode == SubtypingMode.IGNORE_NULL_UNDEFINED
&& (element.isNullType() || element.isVoidType())) {
continue;
}
if (!element.isSubtype(that, implicitImplCache, subtypingMode)) {
return false;
}
}
return true;
}
@Override
public JSType getRestrictedTypeGivenToBooleanOutcome(boolean outcome) {
// gather elements after restriction
UnionTypeBuilder restricted = new UnionTypeBuilder(registry);
for (int i = 0; i < alternatesWithoutStucturalTyping.size(); i++) {
JSType element = alternatesWithoutStucturalTyping.get(i);
restricted.addAlternate(
element.getRestrictedTypeGivenToBooleanOutcome(outcome));
}
return restricted.build();
}
@Override
public BooleanLiteralSet getPossibleToBooleanOutcomes() {
BooleanLiteralSet literals = BooleanLiteralSet.EMPTY;
for (int i = 0; i < alternatesWithoutStucturalTyping.size(); i++) {
JSType element = alternatesWithoutStucturalTyping.get(i);
literals = literals.union(element.getPossibleToBooleanOutcomes());
if (literals == BooleanLiteralSet.BOTH) {
break;
}
}
return literals;
}
@Override
public TypePair getTypesUnderEquality(JSType that) {
UnionTypeBuilder thisRestricted = new UnionTypeBuilder(registry);
UnionTypeBuilder thatRestricted = new UnionTypeBuilder(registry);
for (int i = 0; i < alternatesWithoutStucturalTyping.size(); i++) {
JSType element = alternatesWithoutStucturalTyping.get(i);
TypePair p = element.getTypesUnderEquality(that);
if (p.typeA != null) {
thisRestricted.addAlternate(p.typeA);
}
if (p.typeB != null) {
thatRestricted.addAlternate(p.typeB);
}
}
return new TypePair(
thisRestricted.build(),
thatRestricted.build());
}
@Override
public TypePair getTypesUnderInequality(JSType that) {
UnionTypeBuilder thisRestricted = new UnionTypeBuilder(registry);
UnionTypeBuilder thatRestricted = new UnionTypeBuilder(registry);
for (int i = 0; i < alternatesWithoutStucturalTyping.size(); i++) {
JSType element = alternatesWithoutStucturalTyping.get(i);
TypePair p = element.getTypesUnderInequality(that);
if (p.typeA != null) {
thisRestricted.addAlternate(p.typeA);
}
if (p.typeB != null) {
thatRestricted.addAlternate(p.typeB);
}
}
return new TypePair(
thisRestricted.build(),
thatRestricted.build());
}
@Override
public TypePair getTypesUnderShallowInequality(JSType that) {
UnionTypeBuilder thisRestricted = new UnionTypeBuilder(registry);
UnionTypeBuilder thatRestricted = new UnionTypeBuilder(registry);
for (int i = 0; i < alternatesWithoutStucturalTyping.size(); i++) {
JSType element = alternatesWithoutStucturalTyping.get(i);
TypePair p = element.getTypesUnderShallowInequality(that);
if (p.typeA != null) {
thisRestricted.addAlternate(p.typeA);
}
if (p.typeB != null) {
thatRestricted.addAlternate(p.typeB);
}
}
return new TypePair(
thisRestricted.build(),
thatRestricted.build());
}
@Override
public <T> T visit(Visitor<T> visitor) {
return visitor.caseUnionType(this);
}
@Override <T> T visit(RelationshipVisitor<T> visitor, JSType that) {
return visitor.caseUnionType(this, that);
}
@Override
JSType resolveInternal(ErrorReporter reporter) {
setResolvedTypeInternal(this); // for circularly defined types.
for (int i = 0; i < alternatesWithoutStucturalTyping.size(); i++) {
JSType alternate = alternatesWithoutStucturalTyping.get(i);
alternate.resolve(reporter);
}
// Ensure the union is in a normalized state.
rebuildAlternates();
return this;
}
@Override
public String toDebugHashCodeString() {
List<String> hashCodes = new ArrayList<>();
for (JSType a : alternatesWithoutStucturalTyping) {
hashCodes.add(a.toDebugHashCodeString());
}
return "{(" + Joiner.on(",").join(hashCodes) + ")}";
}
@Override
public boolean setValidator(Predicate<JSType> validator) {
for (int i = 0; i < alternatesWithoutStucturalTyping.size(); i++) {
JSType a = alternatesWithoutStucturalTyping.get(i);
a.setValidator(validator);
}
return true;
}
@Override
public JSType collapseUnion() {
JSType currentValue = null;
ObjectType currentCommonSuper = null;
for (int i = 0; i < alternatesWithoutStucturalTyping.size(); i++) {
JSType a = alternatesWithoutStucturalTyping.get(i);
if (a.isUnknownType()) {
return getNativeType(JSTypeNative.UNKNOWN_TYPE);
}
ObjectType obj = a.toObjectType();
if (obj == null) {
if (currentValue == null && currentCommonSuper == null) {
// If obj is not an object, then it must be a value.
currentValue = a;
} else {
// Multiple values and objects will always collapse to the ALL_TYPE.
return getNativeType(JSTypeNative.ALL_TYPE);
}
} else if (currentValue != null) {
// Values and objects will always collapse to the ALL_TYPE.
return getNativeType(JSTypeNative.ALL_TYPE);
} else if (currentCommonSuper == null) {
currentCommonSuper = obj;
} else {
currentCommonSuper =
registry.findCommonSuperObject(currentCommonSuper, obj);
}
}
return currentCommonSuper;
}
@Override
public void matchConstraint(JSType constraint) {
for (int i = 0; i < alternatesWithoutStucturalTyping.size(); i++) {
JSType alternate = alternatesWithoutStucturalTyping.get(i);
alternate.matchConstraint(constraint);
}
}
@Override
public boolean hasAnyTemplateTypesInternal() {
for (int i = 0; i < alternatesWithoutStucturalTyping.size(); i++) {
JSType alternate = alternatesWithoutStucturalTyping.get(i);
if (alternate.hasAnyTemplateTypes()) {
return true;
}
}
return false;
}
}