@samitouri / QOS-React-2 / commits / eda7788c48

InferReactiveIdentifiers uses IdentifierId

This is to help prep for @poteto's renaming PR. To make that PR work we generally need to use IdentifierId to distinguish "the same identifier" rather than Identifier object identity.

Joe Savona committed Feb 14, 2023 at 15:44 UTC eda7788c4861e3bd55056a9984e78c9adef99a3f
2 files changed +15 -15
compiler/forget/src/ReactiveScopes/InferReactiveIdentifiers.ts
+10 -10
@@ -7,7 +7,7 @@
7
8 import {
9 Effect,
10 - Identifier,
10 + IdentifierId,
11 ReactiveFunction,
12 ReactiveInstruction,
13 } from "../HIR/HIR";
@@ -18,7 +18,7 @@ import {
18 visitReactiveFunction,
19 } from "./visitors";
20
21 -type IdentifierReactivity = Map<Identifier, boolean>;
21 +type IdentifierReactivity = Map<IdentifierId, boolean>;
22 class Environment extends ReactiveFunctionVisitor<IdentifierReactivity> {
23 override visitInstruction(
24 instr: ReactiveInstruction,
@@ -26,7 +26,7 @@ class Environment extends ReactiveFunctionVisitor<IdentifierReactivity> {
26 ) {
27 this.traverseInstruction(instr, reactivityMap);
28 const lval = instr.lvalue;
29 - if (lval == null || reactivityMap.get(lval.place.identifier) === true) {
29 + if (lval == null || reactivityMap.get(lval.place.identifier.id) === true) {
30 return;
31 }
32 const { value } = instr;
@@ -35,7 +35,7 @@ class Environment extends ReactiveFunctionVisitor<IdentifierReactivity> {
35 // We currently treat free variables (from module or global scope) as
36 // non-reactive. We may later want type information about specific
37 // free variables, or a toggle `treatFreeVarsAsReactive`.
38 - if (reactivityMap.get(operand.identifier)) {
38 + if (reactivityMap.get(operand.identifier.id)) {
39 hasReactiveInput = true;
40 break;
41 }
@@ -51,7 +51,7 @@ class Environment extends ReactiveFunctionVisitor<IdentifierReactivity> {
51 hasReactiveInput = true;
52 }
53 }
54 - reactivityMap.set(lval.place.identifier, hasReactiveInput);
54 + reactivityMap.set(lval.place.identifier.id, hasReactiveInput);
55
56 if (hasReactiveInput) {
57 // all mutating effects must also be marked as reactive
@@ -63,8 +63,8 @@ class Environment extends ReactiveFunctionVisitor<IdentifierReactivity> {
63 // Explicitly compare to `false` here, since absence from the
64 // map indicates a free variable
65 // todo [@mofeiZ] add knowledge about free variables
66 - if (reactivityMap.get(operand.identifier) === false) {
67 - reactivityMap.set(operand.identifier, true);
66 + if (reactivityMap.get(operand.identifier.id) === false) {
67 + reactivityMap.set(operand.identifier.id, true);
68 }
69 }
70 }
@@ -111,15 +111,15 @@ class Environment extends ReactiveFunctionVisitor<IdentifierReactivity> {
111 */
112 export function inferReactiveIdentifiers(
113 fn: ReactiveFunction
114 -): Set<Identifier> {
114 +): Set<IdentifierId> {
115 const visitor = new Environment();
116 const reactivityMap: IdentifierReactivity = new Map();
117 for (const param of fn.params) {
118 - reactivityMap.set(param.identifier, true);
118 + reactivityMap.set(param.identifier.id, true);
119 }
120 visitReactiveFunction(fn, visitor, reactivityMap);
121
122 - const result = new Set<Identifier>();
122 + const result = new Set<IdentifierId>();
123 reactivityMap.forEach((isReactive, id) => {
124 if (isReactive) result.add(id);
125 });
compiler/forget/src/ReactiveScopes/PruneNonReactiveDependencies.ts
+5 -5
@@ -5,7 +5,7 @@
5 * LICENSE file in the root directory of this source tree.
6 */
7
8 -import { Identifier, ReactiveFunction, ReactiveScopeBlock } from "../HIR";
8 +import { IdentifierId, ReactiveFunction, ReactiveScopeBlock } from "../HIR";
9 import { inferReactiveIdentifiers } from "./InferReactiveIdentifiers";
10 import { ReactiveFunctionVisitor, visitReactiveFunction } from "./visitors";
11
@@ -20,13 +20,13 @@ export function pruneNonReactiveDependencies(fn: ReactiveFunction): void {
20 visitReactiveFunction(fn, new Visitor(), state);
21 }
22
23 -type State = Set<Identifier>;
23 +type State = Set<IdentifierId>;
24
25 class Visitor extends ReactiveFunctionVisitor<State> {
26 override visitScope(scope: ReactiveScopeBlock, state: State): void {
27 this.traverseScope(scope, state);
28 for (const dep of scope.scope.dependencies) {
29 - const isReactive = state.has(dep.place.identifier);
29 + const isReactive = state.has(dep.place.identifier.id);
30 if (!isReactive) {
31 scope.scope.dependencies.delete(dep);
32 }
@@ -34,12 +34,12 @@ class Visitor extends ReactiveFunctionVisitor<State> {
34 if (scope.scope.dependencies.size === 0) {
35 // If a scope has no dependencies, then its declarations are all non-reactive
36 for (const [, declaration] of scope.scope.declarations) {
37 - state.delete(declaration);
37 + state.delete(declaration.id);
38 }
39 } else {
40 // otherwise, all the scope's declarations are reactive
41 for (const [, declaration] of scope.scope.declarations) {
42 - state.add(declaration);
42 + state.add(declaration.id);
43 }
44 }
45 }