[typer] Add PhiType to represent Phis
Sathya Gunasekaran committed
Jan 11, 2023 at 11:30 UTC
1910ede50066ad6a1e4185d026fe24f2b0d211d5
1 file changed
+24
-1
compiler/forget/src/HIR/HIR.ts
+24
-1
@@ -487,6 +487,7 @@ export type Type =
487
| PrimitiveType
488
| FunctionType
489
| ObjectType
490
+ | PhiType
491
| PolyType
492
| TypeVar;
493
export type PrimitiveType = { kind: "Primitive" };
@@ -501,6 +502,10 @@ export type TypeVar = {
502
export type PolyType = {
503
kind: "Poly";
504
};
505
+export type PhiType = {
506
+ kind: "Phi";
507
+ operands: Array<Type>;
508
+};
509
510
/**
511
* Simulated opaque type for TypeId to prevent using normal numbers as ids
@@ -532,7 +537,8 @@ export function typeEquals(tA: Type, tB: Type): boolean {
537
funcTypeEquals(tA, tB) ||
538
objectTypeEquals(tA, tB) ||
539
primitiveTypeEquals(tA, tB) ||
535
- polyTypeEquals(tA, tB)
540
+ polyTypeEquals(tA, tB) ||
541
+ phiTypeEquals(tA, tB)
542
);
543
}
544
@@ -559,6 +565,23 @@ function funcTypeEquals(tA: Type, tB: Type): boolean {
565
return tA.kind === "Function" && tB.kind === "Function";
566
}
567
568
+function phiTypeEquals(tA: Type, tB: Type): boolean {
569
+ if (tA.kind === "Phi" && tB.kind === "Phi") {
570
+ if (tA.operands.length !== tB.operands.length) {
571
+ return false;
572
+ }
573
+
574
+ let operands = new Set(tA.operands);
575
+ for (let i = 0; i < tB.operands.length; i++) {
576
+ if (!operands.has(tB.operands[i])) {
577
+ return false;
578
+ }
579
+ }
580
+ }
581
+
582
+ return false;
583
+}
584
+
585
export function isObjectType(id: Identifier): boolean {
586
return id.type.kind === "Object";
587
}