[hir] Use types to infer constant declarations

This commit is contained in:
Sathya Gunasekaran
2023-01-16 15:34:24 +00:00
parent 6ea0b6a42e
commit 39eb89f65e
2 changed files with 11 additions and 35 deletions
@@ -10,6 +10,7 @@ import {
InstructionId,
InstructionKind,
InstructionValue,
isPrimitiveType,
LValue,
makeInstructionId,
Place,
@@ -287,7 +288,9 @@ function visitInstruction(context: Context, instr: ReactiveInstruction): void {
const range = lvalue.place.identifier.mutableRange;
// TODO: only assign Const if the value is never reassigned
const kind =
range.end === range.start + 1 ? valueKind(instr.value) : DeclKind.Dynamic;
range.end === range.start + 1
? valueKind(lvalue.place.identifier)
: DeclKind.Dynamic;
context.declare(lvalue.place.identifier, {
kind,
id: lvalue.place.identifier.mutableRange.start,
@@ -295,31 +298,6 @@ function visitInstruction(context: Context, instr: ReactiveInstruction): void {
}
}
function valueKind(value: InstructionValue): DeclKind {
switch (value.kind) {
case "BinaryExpression":
case "JSXText":
case "Primitive": {
return DeclKind.Const;
}
case "ComputedLoad":
case "ComputedStore":
case "PropertyStore":
case "PropertyLoad":
case "Identifier":
case "ArrayExpression":
case "CallExpression":
case "JsxExpression":
case "JsxFragment":
case "NewExpression":
case "ObjectExpression":
case "FunctionExpression":
case "OtherStatement":
case "UnaryExpression": {
return DeclKind.Dynamic;
}
default: {
assertExhaustive(value, `Unexpected value kind '${(value as any).kind}'`);
}
}
function valueKind(id: Identifier): DeclKind {
return isPrimitiveType(id) ? DeclKind.Const : DeclKind.Dynamic;
}
@@ -41,20 +41,18 @@ function component() {
o = $[1];
}
const c_2 = $[2] !== p;
const c_3 = $[3] !== o;
const c_2 = $[2] !== o;
let x;
if (c_2 || c_3) {
if (c_2) {
x = {};
x.t = p;
const z = x.t;
x.t = o;
$[2] = p;
$[3] = o;
$[4] = x;
$[2] = o;
$[3] = x;
} else {
x = $[4];
x = $[3];
}
const y = x.t;