-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathType.ts
879 lines (744 loc) · 29.4 KB
/
Type.ts
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
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
import {
iterableEvery,
iterableFind,
iterableSome,
toReadonlySet,
hashCodeOf,
areEqual,
mapMap,
setMap,
mapSortByKey,
mapSome,
mapFilter,
setSortBy,
setFilter,
setUnionInto,
mapSortToArray,
definedMap,
hashCodeInit,
addHashCode,
hasOwnProperty,
mapFromObject
} from "collection-utils";
import { defined, panic, assert } from "./support/Support";
import { TypeReconstituter, BaseGraphRewriteBuilder } from "./GraphRewriting";
import { TypeNames, namesTypeAttributeKind } from "./TypeNames";
import { TypeAttributes } from "./TypeAttributes";
import { messageAssert } from "./Messages";
import { TypeRef, attributesForTypeRef, derefTypeRef, TypeGraph, typeRefIndex } from "./TypeGraph";
/**
* `jsonSchema` is the `format` to be used to represent this string type in
* JSON Schema. It's ok to "invent" a new one if the JSON Schema standard doesn't
* have that particular type yet.
*
* For transformed type kinds that map to an existing primitive type, `primitive`
* must specify that type kind.
*/
export type TransformedStringTypeTargets = {
jsonSchema: string;
primitive: PrimitiveNonStringTypeKind | undefined;
};
/**
* All the transformed string type kinds and the JSON Schema formats and
* primitive type kinds they map to. Not all transformed string types map to
* primitive types. Date-time types, for example, stand on their own, but
* stringified integers map to integers.
*/
export const transformedStringTypeTargetTypeKinds = {
date: { jsonSchema: "date", primitive: undefined },
time: { jsonSchema: "time", primitive: undefined },
"date-time": { jsonSchema: "date-time", primitive: undefined },
uuid: { jsonSchema: "uuid", primitive: undefined },
"integer-string": { jsonSchema: "integer", primitive: "integer" } as TransformedStringTypeTargets,
"bool-string": { jsonSchema: "boolean", primitive: "bool" } as TransformedStringTypeTargets
};
export const transformedStringTypeTargetTypeKindsMap = mapFromObject(transformedStringTypeTargetTypeKinds as {
[kind: string]: TransformedStringTypeTargets;
});
export type TransformedStringTypeKind = keyof typeof transformedStringTypeTargetTypeKinds;
export type PrimitiveStringTypeKind = "string" | TransformedStringTypeKind;
export type PrimitiveNonStringTypeKind = "none" | "any" | "null" | "bool" | "integer" | "double";
export type PrimitiveTypeKind = PrimitiveNonStringTypeKind | PrimitiveStringTypeKind;
export type NamedTypeKind = "class" | "enum" | "union";
export type TypeKind = PrimitiveTypeKind | NamedTypeKind | "array" | "object" | "map" | "intersection";
export type ObjectTypeKind = "object" | "map" | "class";
export const transformedStringTypeKinds = new Set(
Object.getOwnPropertyNames(transformedStringTypeTargetTypeKinds)
) as ReadonlySet<TransformedStringTypeKind>;
export function isPrimitiveStringTypeKind(kind: string): kind is PrimitiveStringTypeKind {
return kind === "string" || hasOwnProperty(transformedStringTypeTargetTypeKinds, kind);
}
export function targetTypeKindForTransformedStringTypeKind(
kind: PrimitiveStringTypeKind
): PrimitiveNonStringTypeKind | undefined {
const target = transformedStringTypeTargetTypeKindsMap.get(kind);
if (target === undefined) return undefined;
return target.primitive;
}
export function isNumberTypeKind(kind: TypeKind): kind is "integer" | "double" {
return kind === "integer" || kind === "double";
}
export function isPrimitiveTypeKind(kind: TypeKind): kind is PrimitiveTypeKind {
if (isPrimitiveStringTypeKind(kind)) return true;
if (isNumberTypeKind(kind)) return true;
return kind === "none" || kind === "any" || kind === "null" || kind === "bool";
}
function triviallyStructurallyCompatible(x: Type, y: Type): boolean {
if (x.index === y.index) return true;
if (x.kind === "none" || y.kind === "none") return true;
return false;
}
export class TypeIdentity {
private readonly _hashCode: number;
constructor(private readonly _kind: TypeKind, private readonly _components: ReadonlyArray<any>) {
let h = hashCodeInit;
h = addHashCode(h, hashCodeOf(this._kind));
for (const c of _components) {
h = addHashCode(h, hashCodeOf(c));
}
this._hashCode = h;
}
equals(other: any): boolean {
if (!(other instanceof TypeIdentity)) return false;
if (this._kind !== other._kind) return false;
const n = this._components.length;
assert(n === other._components.length, "Components of a type kind's identity must have the same length");
for (let i = 0; i < n; i++) {
if (!areEqual(this._components[i], other._components[i])) return false;
}
return true;
}
hashCode(): number {
return this._hashCode;
}
}
// undefined in case the identity is unique
export type MaybeTypeIdentity = TypeIdentity | undefined;
export abstract class Type {
constructor(readonly typeRef: TypeRef, protected readonly graph: TypeGraph, readonly kind: TypeKind) {}
get index(): number {
return typeRefIndex(this.typeRef);
}
// This must return a newly allocated set
abstract getNonAttributeChildren(): Set<Type>;
getChildren(): ReadonlySet<Type> {
let result = this.getNonAttributeChildren();
for (const [k, v] of this.getAttributes()) {
if (k.children === undefined) continue;
setUnionInto(result, k.children(v));
}
return result;
}
getAttributes(): TypeAttributes {
return attributesForTypeRef(this.typeRef, this.graph);
}
get hasNames(): boolean {
return namesTypeAttributeKind.tryGetInAttributes(this.getAttributes()) !== undefined;
}
getNames(): TypeNames {
return defined(namesTypeAttributeKind.tryGetInAttributes(this.getAttributes()));
}
getCombinedName(): string {
return this.getNames().combinedName;
}
abstract get isNullable(): boolean;
// FIXME: Remove `isPrimitive`
abstract isPrimitive(): this is PrimitiveType;
abstract get identity(): MaybeTypeIdentity;
abstract reconstitute<T extends BaseGraphRewriteBuilder>(
builder: TypeReconstituter<T>,
canonicalOrder: boolean
): void;
get debugPrintKind(): string {
return this.kind;
}
equals(other: any): boolean {
if (!(other instanceof Type)) return false;
return this.typeRef === other.typeRef;
}
hashCode(): number {
return hashCodeOf(this.typeRef);
}
// This will only ever be called when `this` and `other` are not
// equal, but `this.kind === other.kind`.
protected abstract structuralEqualityStep(
other: Type,
conflateNumbers: boolean,
queue: (a: Type, b: Type) => boolean
): boolean;
structurallyCompatible(other: Type, conflateNumbers: boolean = false): boolean {
function kindsCompatible(kind1: TypeKind, kind2: TypeKind): boolean {
if (kind1 === kind2) return true;
if (!conflateNumbers) return false;
if (kind1 === "integer") return kind2 === "double";
if (kind1 === "double") return kind2 === "integer";
return false;
}
if (triviallyStructurallyCompatible(this, other)) return true;
if (!kindsCompatible(this.kind, other.kind)) return false;
const workList: [Type, Type][] = [[this, other]];
// This contains a set of pairs which are the type pairs
// we have already determined to be equal. We can't just
// do comparison recursively because types can have cycles.
const done: [number, number][] = [];
let failed: boolean;
const queue = (x: Type, y: Type): boolean => {
if (triviallyStructurallyCompatible(x, y)) return true;
if (!kindsCompatible(x.kind, y.kind)) {
failed = true;
return false;
}
workList.push([x, y]);
return true;
};
while (workList.length > 0) {
let [a, b] = defined(workList.pop());
if (a.index > b.index) {
[a, b] = [b, a];
}
if (!a.isPrimitive()) {
let ai = a.index;
let bi = b.index;
let found = false;
for (const [dai, dbi] of done) {
if (dai === ai && dbi === bi) {
found = true;
break;
}
}
if (found) continue;
done.push([ai, bi]);
}
failed = false;
if (!a.structuralEqualityStep(b, conflateNumbers, queue)) return false;
if (failed) return false;
}
return true;
}
getParentTypes(): ReadonlySet<Type> {
return this.graph.getParentsOfType(this);
}
getAncestorsNotInSet(set: ReadonlySet<TypeRef>): ReadonlySet<Type> {
const workList: Type[] = [this];
const processed = new Set<Type>();
const ancestors = new Set<Type>();
for (;;) {
const t = workList.pop();
if (t === undefined) break;
const parents = t.getParentTypes();
console.log(`${parents.size} parents`);
for (const p of parents) {
if (processed.has(p)) continue;
processed.add(p);
if (set.has(p.typeRef)) {
console.log(`adding ${p.kind}`);
workList.push(p);
} else {
console.log(`found ${p.kind}`);
ancestors.add(p);
}
}
}
return ancestors;
}
}
function hasUniqueIdentityAttributes(attributes: TypeAttributes): boolean {
return mapSome(attributes, (v, ta) => ta.requiresUniqueIdentity(v));
}
function identityAttributes(attributes: TypeAttributes): TypeAttributes {
return mapFilter(attributes, (_, kind) => kind.inIdentity);
}
export function primitiveTypeIdentity(kind: PrimitiveTypeKind, attributes: TypeAttributes): MaybeTypeIdentity {
if (hasUniqueIdentityAttributes(attributes)) return undefined;
return new TypeIdentity(kind, [identityAttributes(attributes)]);
}
export class PrimitiveType extends Type {
// @ts-ignore: This is initialized in the Type constructor
readonly kind: PrimitiveTypeKind;
get isNullable(): boolean {
return this.kind === "null" || this.kind === "any" || this.kind === "none";
}
isPrimitive(): this is PrimitiveType {
return true;
}
getNonAttributeChildren(): Set<Type> {
return new Set();
}
get identity(): MaybeTypeIdentity {
return primitiveTypeIdentity(this.kind, this.getAttributes());
}
reconstitute<T extends BaseGraphRewriteBuilder>(builder: TypeReconstituter<T>): void {
builder.getPrimitiveType(this.kind);
}
protected structuralEqualityStep(
_other: Type,
_conflateNumbers: boolean,
_queue: (a: Type, b: Type) => boolean
): boolean {
return true;
}
}
export function arrayTypeIdentity(attributes: TypeAttributes, itemsRef: TypeRef): MaybeTypeIdentity {
if (hasUniqueIdentityAttributes(attributes)) return undefined;
return new TypeIdentity("array", [identityAttributes(attributes), itemsRef]);
}
export class ArrayType extends Type {
// @ts-ignore: This is initialized in the Type constructor
readonly kind: "array";
constructor(typeRef: TypeRef, graph: TypeGraph, private _itemsRef?: TypeRef) {
super(typeRef, graph, "array");
}
setItems(itemsRef: TypeRef) {
if (this._itemsRef !== undefined) {
return panic("Can only set array items once");
}
this._itemsRef = itemsRef;
}
private getItemsRef(): TypeRef {
if (this._itemsRef === undefined) {
return panic("Array items accessed before they were set");
}
return this._itemsRef;
}
get items(): Type {
return derefTypeRef(this.getItemsRef(), this.graph);
}
getNonAttributeChildren(): Set<Type> {
return new Set([this.items]);
}
get isNullable(): boolean {
return false;
}
isPrimitive(): this is PrimitiveType {
return false;
}
get identity(): MaybeTypeIdentity {
return arrayTypeIdentity(this.getAttributes(), this.getItemsRef());
}
reconstitute<T extends BaseGraphRewriteBuilder>(builder: TypeReconstituter<T>): void {
const itemsRef = this.getItemsRef();
const maybeItems = builder.lookup(itemsRef);
if (maybeItems === undefined) {
builder.getUniqueArrayType();
builder.setArrayItems(builder.reconstitute(this.getItemsRef()));
} else {
builder.getArrayType(maybeItems);
}
}
protected structuralEqualityStep(
other: ArrayType,
_conflateNumbers: boolean,
queue: (a: Type, b: Type) => boolean
): boolean {
return queue(this.items, other.items);
}
}
export class GenericClassProperty<T> {
constructor(readonly typeData: T, readonly isOptional: boolean) {}
equals(other: any): boolean {
if (!(other instanceof GenericClassProperty)) {
return false;
}
return areEqual(this.typeData, other.typeData) && this.isOptional === other.isOptional;
}
hashCode(): number {
return hashCodeOf(this.typeData) + (this.isOptional ? 17 : 23);
}
}
export class ClassProperty extends GenericClassProperty<TypeRef> {
constructor(typeRef: TypeRef, readonly graph: TypeGraph, isOptional: boolean) {
super(typeRef, isOptional);
}
get typeRef(): TypeRef {
return this.typeData;
}
get type(): Type {
return derefTypeRef(this.typeRef, this.graph);
}
}
function objectTypeIdentify(
kind: ObjectTypeKind,
attributes: TypeAttributes,
properties: ReadonlyMap<string, ClassProperty>,
additionalPropertiesRef: TypeRef | undefined
): MaybeTypeIdentity {
if (hasUniqueIdentityAttributes(attributes)) return undefined;
return new TypeIdentity(kind, [identityAttributes(attributes), properties, additionalPropertiesRef]);
}
export function classTypeIdentity(
attributes: TypeAttributes,
properties: ReadonlyMap<string, ClassProperty>
): MaybeTypeIdentity {
return objectTypeIdentify("class", attributes, properties, undefined);
}
export function mapTypeIdentify(
attributes: TypeAttributes,
additionalPropertiesRef: TypeRef | undefined
): MaybeTypeIdentity {
return objectTypeIdentify("map", attributes, new Map(), additionalPropertiesRef);
}
export class ObjectType extends Type {
// @ts-ignore: This is initialized in the Type constructor
readonly kind: ObjectTypeKind;
constructor(
typeRef: TypeRef,
graph: TypeGraph,
kind: ObjectTypeKind,
readonly isFixed: boolean,
private _properties: ReadonlyMap<string, ClassProperty> | undefined,
private _additionalPropertiesRef: TypeRef | undefined
) {
super(typeRef, graph, kind);
if (kind === "map") {
if (_properties !== undefined) {
assert(_properties.size === 0);
}
assert(!isFixed);
} else if (kind === "class") {
assert(_additionalPropertiesRef === undefined);
} else {
assert(isFixed);
}
}
setProperties(properties: ReadonlyMap<string, ClassProperty>, additionalPropertiesRef: TypeRef | undefined) {
assert(this._properties === undefined, "Tried to set object properties twice");
if (this instanceof MapType) {
assert(properties.size === 0, "Cannot set properties on map type");
}
if (this instanceof ClassType) {
assert(additionalPropertiesRef === undefined, "Cannot set additional properties of class type");
}
this._properties = properties;
this._additionalPropertiesRef = additionalPropertiesRef;
}
getProperties(): ReadonlyMap<string, ClassProperty> {
return defined(this._properties);
}
getSortedProperties(): ReadonlyMap<string, ClassProperty> {
return mapSortByKey(this.getProperties());
}
private getAdditionalPropertiesRef(): TypeRef | undefined {
assert(this._properties !== undefined, "Properties are not set yet");
return this._additionalPropertiesRef;
}
getAdditionalProperties(): Type | undefined {
const tref = this.getAdditionalPropertiesRef();
if (tref === undefined) return undefined;
return derefTypeRef(tref, this.graph);
}
getNonAttributeChildren(): Set<Type> {
const types = mapSortToArray(this.getProperties(), (_, k) => k).map(([_, p]) => p.type);
const additionalProperties = this.getAdditionalProperties();
if (additionalProperties !== undefined) {
types.push(additionalProperties);
}
return new Set(types);
}
get isNullable(): boolean {
return false;
}
isPrimitive(): this is PrimitiveType {
return false;
}
get identity(): MaybeTypeIdentity {
if (this.isFixed) return undefined;
return objectTypeIdentify(
this.kind,
this.getAttributes(),
this.getProperties(),
this.getAdditionalPropertiesRef()
);
}
reconstitute<T extends BaseGraphRewriteBuilder>(builder: TypeReconstituter<T>, canonicalOrder: boolean): void {
const sortedProperties = this.getSortedProperties();
const propertiesInNewOrder = canonicalOrder ? sortedProperties : this.getProperties();
const maybePropertyTypes = builder.lookupMap(mapMap(sortedProperties, cp => cp.typeRef));
const maybeAdditionalProperties = definedMap(this._additionalPropertiesRef, r => builder.lookup(r));
if (
maybePropertyTypes !== undefined &&
(maybeAdditionalProperties !== undefined || this._additionalPropertiesRef === undefined)
) {
const properties = mapMap(propertiesInNewOrder, (cp, n) =>
builder.makeClassProperty(defined(maybePropertyTypes.get(n)), cp.isOptional)
);
switch (this.kind) {
case "object":
assert(this.isFixed);
builder.getObjectType(properties, maybeAdditionalProperties);
break;
case "map":
builder.getMapType(defined(maybeAdditionalProperties));
break;
case "class":
if (this.isFixed) {
builder.getUniqueClassType(true, properties);
} else {
builder.getClassType(properties);
}
break;
default:
return panic(`Invalid object type kind ${this.kind}`);
}
} else {
switch (this.kind) {
case "object":
assert(this.isFixed);
builder.getUniqueObjectType(undefined, undefined);
break;
case "map":
builder.getUniqueMapType();
break;
case "class":
builder.getUniqueClassType(this.isFixed, undefined);
break;
default:
return panic(`Invalid object type kind ${this.kind}`);
}
const reconstitutedTypes = mapMap(sortedProperties, cp => builder.reconstitute(cp.typeRef));
const properties = mapMap(propertiesInNewOrder, (cp, n) =>
builder.makeClassProperty(defined(reconstitutedTypes.get(n)), cp.isOptional)
);
const additionalProperties = definedMap(this._additionalPropertiesRef, r => builder.reconstitute(r));
builder.setObjectProperties(properties, additionalProperties);
}
}
protected structuralEqualityStep(
other: ObjectType,
_conflateNumbers: boolean,
queue: (a: Type, b: Type) => boolean
): boolean {
const pa = this.getProperties();
const pb = other.getProperties();
if (pa.size !== pb.size) return false;
let failed = false;
for (const [name, cpa] of pa) {
const cpb = pb.get(name);
if (cpb === undefined || cpa.isOptional !== cpb.isOptional || !queue(cpa.type, cpb.type)) {
failed = true;
return false;
}
}
if (failed) return false;
const thisAdditionalProperties = this.getAdditionalProperties();
const otherAdditionalProperties = other.getAdditionalProperties();
if ((thisAdditionalProperties === undefined) !== (otherAdditionalProperties === undefined)) return false;
if (thisAdditionalProperties === undefined || otherAdditionalProperties === undefined) return true;
return queue(thisAdditionalProperties, otherAdditionalProperties);
}
}
export class ClassType extends ObjectType {
// @ts-ignore: This is initialized in the Type constructor
kind: "class";
constructor(
typeRef: TypeRef,
graph: TypeGraph,
isFixed: boolean,
properties: ReadonlyMap<string, ClassProperty> | undefined
) {
super(typeRef, graph, "class", isFixed, properties, undefined);
}
}
export class MapType extends ObjectType {
// @ts-ignore: This is initialized in the Type constructor
readonly kind: "map";
constructor(typeRef: TypeRef, graph: TypeGraph, valuesRef: TypeRef | undefined) {
super(typeRef, graph, "map", false, definedMap(valuesRef, () => new Map()), valuesRef);
}
// FIXME: Remove and use `getAdditionalProperties()` instead.
get values(): Type {
return defined(this.getAdditionalProperties());
}
}
export function enumTypeIdentity(attributes: TypeAttributes, cases: ReadonlySet<string>): MaybeTypeIdentity {
if (hasUniqueIdentityAttributes(attributes)) return undefined;
return new TypeIdentity("enum", [identityAttributes(attributes), cases]);
}
export class EnumType extends Type {
// @ts-ignore: This is initialized in the Type constructor
kind: "enum";
constructor(typeRef: TypeRef, graph: TypeGraph, readonly cases: ReadonlySet<string>) {
super(typeRef, graph, "enum");
}
get isNullable(): boolean {
return false;
}
isPrimitive(): this is PrimitiveType {
return false;
}
get identity(): MaybeTypeIdentity {
return enumTypeIdentity(this.getAttributes(), this.cases);
}
getNonAttributeChildren(): Set<Type> {
return new Set();
}
reconstitute<T extends BaseGraphRewriteBuilder>(builder: TypeReconstituter<T>): void {
builder.getEnumType(this.cases);
}
protected structuralEqualityStep(
other: EnumType,
_conflateNumbers: boolean,
_queue: (a: Type, b: Type) => void
): boolean {
return areEqual(this.cases, other.cases);
}
}
export function setOperationCasesEqual(
typesA: Iterable<Type>,
typesB: Iterable<Type>,
conflateNumbers: boolean,
membersEqual: (a: Type, b: Type) => boolean
): boolean {
const ma = toReadonlySet(typesA);
const mb = toReadonlySet(typesB);
if (ma.size !== mb.size) return false;
return iterableEvery(ma, ta => {
const tb = iterableFind(mb, t => t.kind === ta.kind);
if (tb !== undefined) {
if (membersEqual(ta, tb)) return true;
}
if (conflateNumbers) {
if (ta.kind === "integer" && iterableSome(mb, t => t.kind === "double")) return true;
if (ta.kind === "double" && iterableSome(mb, t => t.kind === "integer")) return true;
}
return false;
});
}
export function setOperationTypeIdentity(
kind: TypeKind,
attributes: TypeAttributes,
memberRefs: ReadonlySet<TypeRef>
): MaybeTypeIdentity {
if (hasUniqueIdentityAttributes(attributes)) return undefined;
return new TypeIdentity(kind, [identityAttributes(attributes), memberRefs]);
}
export function unionTypeIdentity(attributes: TypeAttributes, memberRefs: ReadonlySet<TypeRef>): MaybeTypeIdentity {
return setOperationTypeIdentity("union", attributes, memberRefs);
}
export function intersectionTypeIdentity(
attributes: TypeAttributes,
memberRefs: ReadonlySet<TypeRef>
): MaybeTypeIdentity {
return setOperationTypeIdentity("intersection", attributes, memberRefs);
}
export abstract class SetOperationType extends Type {
constructor(typeRef: TypeRef, graph: TypeGraph, kind: TypeKind, private _memberRefs?: ReadonlySet<TypeRef>) {
super(typeRef, graph, kind);
}
setMembers(memberRefs: ReadonlySet<TypeRef>): void {
if (this._memberRefs !== undefined) {
return panic("Can only set map members once");
}
this._memberRefs = memberRefs;
}
protected getMemberRefs(): ReadonlySet<TypeRef> {
if (this._memberRefs === undefined) {
return panic("Map members accessed before they were set");
}
return this._memberRefs;
}
get members(): ReadonlySet<Type> {
return setMap(this.getMemberRefs(), tref => derefTypeRef(tref, this.graph));
}
get sortedMembers(): ReadonlySet<Type> {
return this.getNonAttributeChildren();
}
getNonAttributeChildren(): Set<Type> {
// FIXME: We're assuming no two members of the same kind.
return setSortBy(this.members, t => t.kind);
}
isPrimitive(): this is PrimitiveType {
return false;
}
get identity(): MaybeTypeIdentity {
return setOperationTypeIdentity(this.kind, this.getAttributes(), this.getMemberRefs());
}
protected reconstituteSetOperation<T extends BaseGraphRewriteBuilder>(
builder: TypeReconstituter<T>,
canonicalOrder: boolean,
getType: (members: ReadonlySet<TypeRef> | undefined) => void
): void {
const sortedMemberRefs = mapMap(this.sortedMembers.entries(), t => t.typeRef);
const membersInOrder = canonicalOrder ? this.sortedMembers : this.members;
const maybeMembers = builder.lookupMap(sortedMemberRefs);
if (maybeMembers === undefined) {
getType(undefined);
const reconstituted = builder.reconstituteMap(sortedMemberRefs);
builder.setSetOperationMembers(setMap(membersInOrder, t => defined(reconstituted.get(t))));
} else {
getType(setMap(membersInOrder, t => defined(maybeMembers.get(t))));
}
}
protected structuralEqualityStep(
other: SetOperationType,
conflateNumbers: boolean,
queue: (a: Type, b: Type) => boolean
): boolean {
return setOperationCasesEqual(this.members, other.members, conflateNumbers, queue);
}
}
export class IntersectionType extends SetOperationType {
// @ts-ignore: This is initialized in the Type constructor
kind: "intersection";
constructor(typeRef: TypeRef, graph: TypeGraph, memberRefs?: ReadonlySet<TypeRef>) {
super(typeRef, graph, "intersection", memberRefs);
}
get isNullable(): boolean {
return panic("isNullable not implemented for IntersectionType");
}
reconstitute<T extends BaseGraphRewriteBuilder>(builder: TypeReconstituter<T>, canonicalOrder: boolean): void {
this.reconstituteSetOperation(builder, canonicalOrder, members => {
if (members === undefined) {
builder.getUniqueIntersectionType();
} else {
builder.getIntersectionType(members);
}
});
}
}
export class UnionType extends SetOperationType {
// @ts-ignore: This is initialized in the Type constructor
kind: "union";
constructor(typeRef: TypeRef, graph: TypeGraph, memberRefs?: ReadonlySet<TypeRef>) {
super(typeRef, graph, "union", memberRefs);
if (memberRefs !== undefined) {
messageAssert(memberRefs.size > 0, "IRNoEmptyUnions", {});
}
}
setMembers(memberRefs: ReadonlySet<TypeRef>): void {
messageAssert(memberRefs.size > 0, "IRNoEmptyUnions", {});
super.setMembers(memberRefs);
}
get stringTypeMembers(): ReadonlySet<Type> {
return setFilter(this.members, t => isPrimitiveStringTypeKind(t.kind) || t.kind === "enum");
}
findMember(kind: TypeKind): Type | undefined {
return iterableFind(this.members, t => t.kind === kind);
}
get isNullable(): boolean {
return this.findMember("null") !== undefined;
}
get isCanonical(): boolean {
const members = this.members;
if (members.size <= 1) return false;
const kinds = setMap(members, t => t.kind);
if (kinds.size < members.size) return false;
if (kinds.has("union") || kinds.has("intersection")) return false;
if (kinds.has("none") || kinds.has("any")) return false;
if (kinds.has("string") && kinds.has("enum")) return false;
let numObjectTypes = 0;
if (kinds.has("class")) numObjectTypes += 1;
if (kinds.has("map")) numObjectTypes += 1;
if (kinds.has("object")) numObjectTypes += 1;
if (numObjectTypes > 1) return false;
return true;
}
reconstitute<T extends BaseGraphRewriteBuilder>(builder: TypeReconstituter<T>, canonicalOrder: boolean): void {
this.reconstituteSetOperation(builder, canonicalOrder, members => {
if (members === undefined) {
builder.getUniqueUnionType();
} else {
builder.getUnionType(members);
}
});
}
}