Skip to content

Commit

Permalink
compiler: getGlobalDeclaration() takes a NonLocalBinding
Browse files Browse the repository at this point in the history
No-op refactor to make Environment#getGlobalDeclaration() take a NonLocalBinding instead of just a name. The idea is that in subsequent PRs we can use information about the binding to resolve a type more accurately. For example, we can resolve `Array` differently if its an import or local and not the global Array. Similar for resolving local `useState` differently than the one from React.

ghstack-source-id: c8063e6fb8acdd347a56477d6b06238dd54979b1
Pull Request resolved: #29189
  • Loading branch information
josephsavona committed May 24, 2024
1 parent 5061f31 commit 788ed90
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
Effect,
FunctionType,
IdentifierId,
NonLocalBinding,
PolyType,
ScopeId,
Type,
Expand Down Expand Up @@ -511,7 +512,8 @@ export class Environment {
return this.#hoistedIdentifiers.has(node);
}

getGlobalDeclaration(name: string): Global | null {
getGlobalDeclaration(binding: NonLocalBinding): Global | null {
const name = binding.name;
let resolvedName = name;

if (this.config.hookPattern != null) {
Expand All @@ -534,6 +536,7 @@ export class Environment {
log(() => `Undefined global \`${name}\``);
}
}

return resolvedGlobal;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { Binding, NodePath } from "@babel/traverse";
import * as t from "@babel/types";
import { CompilerError } from "../CompilerError";
import { Environment } from "./Environment";
import { Global } from "./Globals";
import {
BasicBlock,
BlockId,
Expand Down Expand Up @@ -186,25 +185,6 @@ export default class HIRBuilder {
};
}

resolveGlobal(
path: NodePath<t.Identifier | t.JSXIdentifier>
): (Global & { name: string }) | null {
const name = path.node.name;
const resolvedGlobal = this.#env.getGlobalDeclaration(name);
if (resolvedGlobal) {
return {
...resolvedGlobal,
name,
};
} else {
// if env records no global with the given name, load it as an unknown type
return {
kind: "Poly",
name,
};
}
}

#resolveBabelBinding(
path: NodePath<t.Identifier | t.JSXIdentifier>
): Binding | null {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ function collectTemporaries(
break;
}
case "LoadGlobal": {
const global = env.getGlobalDeclaration(value.binding.name);
const global = env.getGlobalDeclaration(value.binding);
const hookKind = global !== null ? getHookKindForType(env, global) : null;
const lvalId = instr.lvalue.identifier.id;
if (hookKind === "useMemo" || hookKind === "useCallback") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ function* generateInstructionTypes(
}

case "LoadGlobal": {
const globalType = env.getGlobalDeclaration(value.binding.name);
const globalType = env.getGlobalDeclaration(value.binding);
if (globalType) {
yield equation(left, globalType);
}
Expand Down

0 comments on commit 788ed90

Please sign in to comment.