@samitouri / QOS-React-2 / commits / 9465cb2100

[hir] Move Types to a separate file

--- Move types around in preparation for next PR #1335. No changes in impl, types, or functions.

Mofei Zhang committed Mar 13, 2023 at 14:52 UTC 9465cb2100bedcb45cab56f6c45c4977bc68b3e4
2 files changed +126 -116
compiler/forget/src/HIR/HIR.ts
+3 -116
@@ -8,7 +8,7 @@
8 import * as t from "@babel/types";
9 import invariant from "invariant";
10 import { Environment } from "./Environment";
11 -import { Hook } from "./Hooks";
11 +import { Type } from "./Types";
12
13 // *******************************************************************************************
14 // *******************************************************************************************
@@ -763,121 +763,6 @@ export function makeInstructionId(id: number): InstructionId {
763 return id as InstructionId;
764 }
765
766 -export type Type =
767 - | PrimitiveType
768 - | HookType
769 - | FunctionType
770 - | ObjectType
771 - | PhiType
772 - | PolyType
773 - | TypeVar;
774 -export type PrimitiveType = { kind: "Primitive" };
775 -export type FunctionType = {
776 - kind: "Function";
777 -};
778 -export type HookType = {
779 - kind: "Hook";
780 - definition: Hook;
781 -};
782 -export type ObjectType = { kind: "Object" };
783 -export type TypeVar = {
784 - kind: "Type";
785 - id: TypeId;
786 -};
787 -export type PolyType = {
788 - kind: "Poly";
789 -};
790 -export type PhiType = {
791 - kind: "Phi";
792 - operands: Array<Type>;
793 -};
794 -
795 -/**
796 - * Simulated opaque type for TypeId to prevent using normal numbers as ids
797 - * accidentally.
798 - */
799 -const opaqueTypeId = Symbol();
800 -export type TypeId = number & { [opaqueTypeId]: "IdentifierId" };
801 -
802 -export function makeTypeId(id: number): TypeId {
803 - invariant(
804 - id >= 0 && Number.isInteger(id),
805 - "Expected instruction id to be a non-negative integer"
806 - );
807 - return id as TypeId;
808 -}
809 -
810 -let typeCounter = 0;
811 -export function makeType(): TypeVar {
812 - return {
813 - kind: "Type",
814 - id: makeTypeId(typeCounter++),
815 - };
816 -}
817 -
818 -export function typeEquals(tA: Type, tB: Type): boolean {
819 - if (tA.kind !== tB.kind) return false;
820 - return (
821 - typeVarEquals(tA, tB) ||
822 - funcTypeEquals(tA, tB) ||
823 - hookTypeEquals(tA, tB) ||
824 - objectTypeEquals(tA, tB) ||
825 - primitiveTypeEquals(tA, tB) ||
826 - polyTypeEquals(tA, tB) ||
827 - phiTypeEquals(tA, tB)
828 - );
829 -}
830 -
831 -function typeVarEquals(tA: Type, tB: Type): boolean {
832 - if (tA.kind === "Type" && tB.kind === "Type") {
833 - return tA.id === tB.id;
834 - }
835 - return false;
836 -}
837 -
838 -function typeKindCheck(tA: Type, tb: Type, type: string): boolean {
839 - return tA.kind === type && tb.kind === type;
840 -}
841 -
842 -function primitiveTypeEquals(tA: Type, tB: Type): boolean {
843 - return typeKindCheck(tA, tB, "Primitive");
844 -}
845 -
846 -function polyTypeEquals(tA: Type, tB: Type): boolean {
847 - return typeKindCheck(tA, tB, "Poly");
848 -}
849 -
850 -function objectTypeEquals(tA: Type, tB: Type): boolean {
851 - return typeKindCheck(tA, tB, "Object");
852 -}
853 -
854 -function funcTypeEquals(tA: Type, tB: Type): boolean {
855 - return typeKindCheck(tA, tB, "Function");
856 -}
857 -
858 -function hookTypeEquals(tA: Type, tB: Type): boolean {
859 - return (
860 - tA.kind === "Hook" && tB.kind === "Hook" && tA.definition === tB.definition
861 - );
862 -}
863 -
864 -function phiTypeEquals(tA: Type, tB: Type): boolean {
865 - if (tA.kind === "Phi" && tB.kind === "Phi") {
866 - if (tA.operands.length !== tB.operands.length) {
867 - return false;
868 - }
869 -
870 - let operands = new Set(tA.operands);
871 - for (let i = 0; i < tB.operands.length; i++) {
872 - if (!operands.has(tB.operands[i])) {
873 - return false;
874 - }
875 - }
876 - }
877 -
878 - return false;
879 -}
880 -
766 export function isObjectType(id: Identifier): boolean {
767 return id.type.kind === "Object";
768 }
@@ -889,3 +774,5 @@ export function isPrimitiveType(id: Identifier): boolean {
774 export function isHookType(id: Identifier): boolean {
775 return id.type.kind === "Hook";
776 }
777 +
778 +export * from "./Types";
compiler/forget/src/HIR/Types.ts new
+123
@@ -0,0 +1,123 @@
1 +/**
2 + * Copyright (c) Facebook, Inc. and its affiliates.
3 + *
4 + * This source code is licensed under the MIT license found in the
5 + * LICENSE file in the root directory of this source tree.
6 + */
7 +import invariant from "invariant";
8 +import { Hook } from "./Hooks";
9 +
10 +export type Type =
11 + | PrimitiveType
12 + | HookType
13 + | FunctionType
14 + | ObjectType
15 + | PhiType
16 + | PolyType
17 + | TypeVar;
18 +export type PrimitiveType = { kind: "Primitive" };
19 +export type FunctionType = {
20 + kind: "Function";
21 +};
22 +export type HookType = {
23 + kind: "Hook";
24 + definition: Hook;
25 +};
26 +export type ObjectType = { kind: "Object" };
27 +export type TypeVar = {
28 + kind: "Type";
29 + id: TypeId;
30 +};
31 +export type PolyType = {
32 + kind: "Poly";
33 +};
34 +export type PhiType = {
35 + kind: "Phi";
36 + operands: Array<Type>;
37 +};
38 +
39 +/**
40 + * Simulated opaque type for TypeId to prevent using normal numbers as ids
41 + * accidentally.
42 + */
43 +const opaqueTypeId = Symbol();
44 +export type TypeId = number & { [opaqueTypeId]: "IdentifierId" };
45 +
46 +export function makeTypeId(id: number): TypeId {
47 + invariant(
48 + id >= 0 && Number.isInteger(id),
49 + "Expected instruction id to be a non-negative integer"
50 + );
51 + return id as TypeId;
52 +}
53 +
54 +let typeCounter = 0;
55 +export function makeType(): TypeVar {
56 + return {
57 + kind: "Type",
58 + id: makeTypeId(typeCounter++),
59 + };
60 +}
61 +
62 +export function typeEquals(tA: Type, tB: Type): boolean {
63 + if (tA.kind !== tB.kind) return false;
64 + return (
65 + typeVarEquals(tA, tB) ||
66 + funcTypeEquals(tA, tB) ||
67 + hookTypeEquals(tA, tB) ||
68 + objectTypeEquals(tA, tB) ||
69 + primitiveTypeEquals(tA, tB) ||
70 + polyTypeEquals(tA, tB) ||
71 + phiTypeEquals(tA, tB)
72 + );
73 +}
74 +
75 +function typeVarEquals(tA: Type, tB: Type): boolean {
76 + if (tA.kind === "Type" && tB.kind === "Type") {
77 + return tA.id === tB.id;
78 + }
79 + return false;
80 +}
81 +
82 +function typeKindCheck(tA: Type, tb: Type, type: string): boolean {
83 + return tA.kind === type && tb.kind === type;
84 +}
85 +
86 +function primitiveTypeEquals(tA: Type, tB: Type): boolean {
87 + return typeKindCheck(tA, tB, "Primitive");
88 +}
89 +
90 +function polyTypeEquals(tA: Type, tB: Type): boolean {
91 + return typeKindCheck(tA, tB, "Poly");
92 +}
93 +
94 +function objectTypeEquals(tA: Type, tB: Type): boolean {
95 + return typeKindCheck(tA, tB, "Object");
96 +}
97 +
98 +function funcTypeEquals(tA: Type, tB: Type): boolean {
99 + return typeKindCheck(tA, tB, "Function");
100 +}
101 +
102 +function hookTypeEquals(tA: Type, tB: Type): boolean {
103 + return (
104 + tA.kind === "Hook" && tB.kind === "Hook" && tA.definition === tB.definition
105 + );
106 +}
107 +
108 +function phiTypeEquals(tA: Type, tB: Type): boolean {
109 + if (tA.kind === "Phi" && tB.kind === "Phi") {
110 + if (tA.operands.length !== tB.operands.length) {
111 + return false;
112 + }
113 +
114 + let operands = new Set(tA.operands);
115 + for (let i = 0; i < tB.operands.length; i++) {
116 + if (!operands.has(tB.operands[i])) {
117 + return false;
118 + }
119 + }
120 + }
121 +
122 + return false;
123 +}