@samitouri / QOS-React-1 / commits / 76aaf32c55

HIR StoreLocal.type uses babel type

Joe Savona committed Feb 5, 2024 at 21:51 UTC 76aaf32c55c0ff410d6b6bbc02a440dcfd7bfde9
7 files changed +33 -39
compiler/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts
+22 -28
@@ -1615,7 +1615,7 @@ function lowerExpression(
1615 kind: "StoreLocal",
1616 lvalue: { kind: InstructionKind.Const, place: { ...place } },
1617 value: last,
1618 - type: makeType(),
1618 + type: null,
1619 loc: exprLoc,
1620 });
1621 }
@@ -1657,7 +1657,7 @@ function lowerExpression(
1657 kind: "StoreLocal",
1658 lvalue: { kind: InstructionKind.Const, place: { ...place } },
1659 value: consequent,
1660 - type: makeType(),
1660 + type: null,
1661 loc: exprLoc,
1662 });
1663 return {
@@ -1676,7 +1676,7 @@ function lowerExpression(
1676 kind: "StoreLocal",
1677 lvalue: { kind: InstructionKind.Const, place: { ...place } },
1678 value: alternate,
1679 - type: makeType(),
1679 + type: null,
1680 loc: exprLoc,
1681 });
1682 return {
@@ -1727,7 +1727,7 @@ function lowerExpression(
1727 kind: "StoreLocal",
1728 lvalue: { kind: InstructionKind.Const, place: { ...place } },
1729 value: { ...leftPlace },
1730 - type: makeType(),
1730 + type: null,
1731 loc: leftPlace.loc,
1732 });
1733 return {
@@ -1744,7 +1744,7 @@ function lowerExpression(
1744 kind: "StoreLocal",
1745 lvalue: { kind: InstructionKind.Const, place: { ...place } },
1746 value: { ...right },
1747 - type: makeType(),
1747 + type: null,
1748 loc: right.loc,
1749 });
1750 return {
@@ -1853,7 +1853,7 @@ function lowerExpression(
1853 kind: InstructionKind.Reassign,
1854 },
1855 value: { ...binaryPlace },
1856 - type: makeType(),
1856 + type: null,
1857 loc: exprLoc,
1858 });
1859 } else {
@@ -2198,7 +2198,7 @@ function lowerExpression(
2198 kind: "TypeCastExpression",
2199 value: lowerExpressionToTemporary(builder, expr.get("expression")),
2200 typeAnnotation: typeAnnotation.node,
2201 - type: lowerType(builder, typeAnnotation),
2201 + type: lowerType(typeAnnotation.node),
2202 loc: exprLoc,
2203 };
2204 }
@@ -2209,7 +2209,7 @@ function lowerExpression(
2209 kind: "TypeCastExpression",
2210 value: lowerExpressionToTemporary(builder, expr.get("expression")),
2211 typeAnnotation: typeAnnotation.node,
2212 - type: lowerType(builder, typeAnnotation),
2212 + type: lowerType(typeAnnotation.node),
2213 loc: exprLoc,
2214 };
2215 }
@@ -2315,7 +2315,7 @@ function lowerOptionalMemberExpression(
2315 kind: "StoreLocal",
2316 lvalue: { kind: InstructionKind.Const, place: { ...place } },
2317 value: { ...temp },
2318 - type: makeType(),
2318 + type: null,
2319 loc,
2320 });
2321 return {
@@ -2370,7 +2370,7 @@ function lowerOptionalMemberExpression(
2370 kind: "StoreLocal",
2371 lvalue: { kind: InstructionKind.Const, place: { ...place } },
2372 value: { ...temp },
2373 - type: makeType(),
2373 + type: null,
2374 loc,
2375 });
2376 return {
@@ -2427,7 +2427,7 @@ function lowerOptionalCallExpression(
2427 kind: "StoreLocal",
2428 lvalue: { kind: InstructionKind.Const, place: { ...place } },
2429 value: { ...temp },
2430 - type: makeType(),
2430 + type: null,
2431 loc,
2432 });
2433 return {
@@ -2529,7 +2529,7 @@ function lowerOptionalCallExpression(
2529 kind: "StoreLocal",
2530 lvalue: { kind: InstructionKind.Const, place: { ...place } },
2531 value: { ...temp },
2532 - type: makeType(),
2532 + type: null,
2533 loc,
2534 });
2535 return {
@@ -3263,15 +3263,15 @@ function lowerAssignment(
3263 });
3264 } else {
3265 const typeAnnotation = lvalue.get("typeAnnotation");
3266 - let type: Type;
3266 + let type: t.FlowType | t.TSType | null;
3267 if (typeAnnotation.isTSTypeAnnotation()) {
3268 const typePath = typeAnnotation.get("typeAnnotation");
3269 - type = lowerType(builder, typePath);
3269 + type = typePath.node;
3270 } else if (typeAnnotation.isTypeAnnotation()) {
3271 const typePath = typeAnnotation.get("typeAnnotation");
3272 - type = lowerType(builder, typePath);
3272 + type = typePath.node;
3273 } else {
3274 - type = makeType();
3274 + type = null;
3275 }
3276 temporary = lowerValueToTemporary(builder, {
3277 kind: "StoreLocal",
@@ -3584,7 +3584,7 @@ function lowerAssignment(
3584 kind: "StoreLocal",
3585 lvalue: { kind: InstructionKind.Const, place: { ...temp } },
3586 value: { ...defaultValue },
3587 - type: makeType(),
3587 + type: null,
3588 loc,
3589 });
3590 return {
@@ -3601,7 +3601,7 @@ function lowerAssignment(
3601 kind: "StoreLocal",
3602 lvalue: { kind: InstructionKind.Const, place: { ...temp } },
3603 value: { ...value },
3604 - type: makeType(),
3604 + type: null,
3605 loc,
3606 });
3607 return {
@@ -3857,23 +3857,17 @@ function notNull<T>(value: T | null): value is T {
3857 return value !== null;
3858 }
3859
3860 -function lowerType(
3861 - _builder: HIRBuilder,
3862 - path: NodePath<t.FlowType | t.TSType>
3863 -): Type {
3864 - const node = path.node;
3860 +export function lowerType(node: t.FlowType | t.TSType): Type {
3861 switch (node.type) {
3862 case "GenericTypeAnnotation": {
3867 - const typeAnnotation = path as NodePath<t.GenericTypeAnnotation>;
3868 - const id = typeAnnotation.get("id");
3869 - if (id.node.type === "Identifier" && id.node.name === "Array") {
3863 + const id = node.id;
3864 + if (id.type === "Identifier" && id.name === "Array") {
3865 return { kind: "Object", shapeId: BuiltInArrayId };
3866 }
3867 return makeType();
3868 }
3869 case "TSTypeReference": {
3875 - const typeReference = path as NodePath<t.TSTypeReference>;
3876 - const typeName = typeReference.get("typeName").node;
3870 + const typeName = node.typeName;
3871 if (typeName.type === "Identifier" && typeName.name === "Array") {
3872 return { kind: "Object", shapeId: BuiltInArrayId };
3873 }
compiler/packages/babel-plugin-react-forget/src/HIR/HIR.ts
+1 -1
@@ -678,7 +678,7 @@ export type InstructionValue =
678 kind: "StoreLocal";
679 lvalue: LValue;
680 value: Place;
681 - type: Type;
681 + type: t.FlowType | t.TSType | null;
682 loc: SourceLocation;
683 }
684 | {
compiler/packages/babel-plugin-react-forget/src/Inference/InlineImmediatelyInvokedFunctionExpressions.ts
+1 -1
@@ -245,7 +245,7 @@ function rewriteBlock(
245 kind: "StoreLocal",
246 lvalue: { kind: InstructionKind.Reassign, place: { ...returnValue } },
247 value: terminal.value,
248 - type: makeType(),
248 + type: null,
249 loc: terminal.loc,
250 },
251 });
compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/ExtractScopeDeclarationsFromDestructuring.ts
+1 -2
@@ -15,7 +15,6 @@ import {
15 ReactiveFunction,
16 ReactiveInstruction,
17 ReactiveScopeBlock,
18 - makeType,
18 } from "../HIR";
19 import { eachPatternOperand, mapPatternOperands } from "../HIR/visitors";
20 import { ReactiveFunctionTransform, visitReactiveFunction } from "./visitors";
@@ -178,7 +177,7 @@ function transformDestructuring(
177 place: original,
178 },
179 value: temporary,
181 - type: makeType(),
180 + type: null,
181 loc: destructure.loc,
182 },
183 loc: instr.loc,
compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/PropagateEarlyReturns.ts
+2 -3
@@ -16,7 +16,6 @@ import {
16 ReactiveStatement,
17 ReactiveTerminalStatement,
18 makeInstructionId,
19 - makeType,
19 } from "../HIR";
20 import { createTemporaryPlace } from "../HIR/HIRBuilder";
21 import { EARLY_RETURN_SENTINEL } from "./CodegenReactiveFunction";
@@ -221,7 +220,7 @@ class Transform extends ReactiveFunctionTransform<State> {
220 value: {
221 kind: "StoreLocal",
222 loc,
224 - type: makeType(),
223 + type: null,
224 lvalue: {
225 kind: InstructionKind.Let,
226 place: {
@@ -295,7 +294,7 @@ class Transform extends ReactiveFunctionTransform<State> {
294 value: {
295 kind: "StoreLocal",
296 loc,
298 - type: makeType(),
297 + type: null,
298 lvalue: {
299 kind: InstructionKind.Reassign,
300 place: {
compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/PruneHoistedContexts.ts
+1 -2
@@ -11,7 +11,6 @@ import {
11 ReactiveFunction,
12 ReactiveInstruction,
13 ReactiveStatement,
14 - makeType,
14 } from "../HIR";
15 import {
16 ReactiveFunctionTransform,
@@ -60,7 +59,7 @@ class Visitor extends ReactiveFunctionTransform<HoistedIdentifiers> {
59 ...instruction.value.lvalue,
60 kind: InstructionKind.Const,
61 },
63 - type: makeType(),
62 + type: null,
63 kind: "StoreLocal",
64 },
65 },
compiler/packages/babel-plugin-react-forget/src/TypeInference/InferTypes.ts
+5 -2
@@ -8,6 +8,7 @@
8 import * as t from "@babel/types";
9 import { CompilerError } from "../CompilerError";
10 import { Environment } from "../HIR";
11 +import { lowerType } from "../HIR/BuildHIR";
12 import {
13 HIRFunction,
14 Instruction,
@@ -142,8 +143,10 @@ function* generateInstructionTypes(
143 value.lvalue.place.identifier.type,
144 value.value.identifier.type
145 );
145 - yield equation(value.type, value.lvalue.place.identifier.type);
146 - yield equation(left, value.type);
146 + const valueType =
147 + value.type === null ? makeType() : lowerType(value.type);
148 + yield equation(valueType, value.lvalue.place.identifier.type);
149 + yield equation(left, valueType);
150 } else {
151 yield equation(left, value.value.identifier.type);
152 yield equation(