[hir] Update Place.type based on value inference
Sathya Gunasekaran committed
Dec 13, 2022 at 09:17 UTC
74599a92a28e0a5ea7db02afc44911cd2b1aae35
2 files changed
+22
-1
compiler/forget/src/HIR/HIR.ts
+2
@@ -402,4 +402,6 @@ export function makeInstructionId(id: number): InstructionId {
402
403
export enum Type {
404
Any,
405
+ Primitive,
406
+ Object,
407
}
compiler/forget/src/HIR/InferTypes.ts
+20
-1
@@ -1,6 +1,13 @@
1
import invariant from "invariant";
2
import DisjointSet from "./DisjointSet";
3
-import { HIRFunction, Identifier, Instruction, LValue, Place } from "./HIR";
3
+import {
4
+ HIRFunction,
5
+ Identifier,
6
+ Instruction,
7
+ LValue,
8
+ Place,
9
+ Type,
10
+} from "./HIR";
11
import { printInstructionValue } from "./PrintHIR";
12
13
type AbstractValue = AbstractObject | AbstractPrimitive;
@@ -13,6 +20,17 @@ type AbstractPrimitive = {
20
value: number | boolean | string | null | undefined;
21
};
22
23
+function typeOf(value: AbstractValue) {
24
+ switch (value.kind) {
25
+ case "Primitive":
26
+ return Type.Primitive;
27
+ case "Object":
28
+ return Type.Object;
29
+ default:
30
+ return Type.Any;
31
+ }
32
+}
33
+
34
class AbstractState {
35
#values = new Map<Identifier, AbstractValue>();
36
@@ -96,6 +114,7 @@ class AbstractState {
114
// Simple lvalue:
115
// lvalue = alias;
116
// lvalue = alias.memberPath;
117
+ lvalue.place.type = typeOf(value);
118
this.#values.set(lvalue.place.identifier, value);
119
}
120
}