mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
[be] Rename some types for ReactiveFunction
This commit is contained in:
@@ -14,11 +14,11 @@ import {
|
||||
InstructionId,
|
||||
InstructionValue,
|
||||
Place,
|
||||
ReactiveBasicBlock,
|
||||
ReactiveBlock,
|
||||
ReactiveFunction,
|
||||
ReactiveInstruction,
|
||||
ReactiveScope,
|
||||
ReactiveScopeBlock,
|
||||
ReactiveStatement,
|
||||
ReactiveTerminal,
|
||||
ReactiveValueBlock,
|
||||
ScopeId,
|
||||
@@ -41,23 +41,23 @@ export function buildReactiveFunction(fn: HIRFunction): ReactiveFunction {
|
||||
}
|
||||
|
||||
type BlockKind =
|
||||
| { kind: "block"; block: ReactiveBasicBlock }
|
||||
| { kind: "scope"; block: ReactiveBasicBlock; scope: ReactiveScope };
|
||||
| { kind: "block"; block: ReactiveBlock }
|
||||
| { kind: "scope"; block: ReactiveBlock; scope: ReactiveScope };
|
||||
|
||||
class Builder {
|
||||
#instructions: ReactiveBasicBlock;
|
||||
#instructions: ReactiveBlock;
|
||||
#stack: Array<
|
||||
| { kind: "scope"; block: ReactiveBlock }
|
||||
| { kind: "block"; block: ReactiveBasicBlock }
|
||||
| { kind: "scope"; block: ReactiveScopeBlock }
|
||||
| { kind: "block"; block: ReactiveBlock }
|
||||
>;
|
||||
|
||||
constructor() {
|
||||
const block: ReactiveBasicBlock = [];
|
||||
const block: ReactiveBlock = [];
|
||||
this.#instructions = block;
|
||||
this.#stack = [{ kind: "block", block }];
|
||||
}
|
||||
|
||||
append(item: ReactiveInstruction, label: BlockId | undefined): void {
|
||||
append(item: ReactiveStatement, label: BlockId | undefined): void {
|
||||
if (label !== undefined) {
|
||||
invariant(item.kind === "terminal", "Only terminals may have a label");
|
||||
item.label = label;
|
||||
@@ -66,8 +66,8 @@ class Builder {
|
||||
}
|
||||
|
||||
startScope(scope: ReactiveScope): void {
|
||||
const block: ReactiveBlock = {
|
||||
kind: "block",
|
||||
const block: ReactiveScopeBlock = {
|
||||
kind: "scope",
|
||||
scope,
|
||||
instructions: [],
|
||||
};
|
||||
@@ -92,7 +92,7 @@ class Builder {
|
||||
}
|
||||
}
|
||||
|
||||
complete(): ReactiveBasicBlock {
|
||||
complete(): ReactiveBlock {
|
||||
// TODO @josephsavona: debug two failures of this
|
||||
// invariant(
|
||||
// this.#stack.length === 1,
|
||||
@@ -111,12 +111,12 @@ class ReactiveFunctionBuilder
|
||||
implements
|
||||
Visitor<
|
||||
Builder,
|
||||
ReactiveBasicBlock,
|
||||
ReactiveBlock,
|
||||
ReactiveValueBlock,
|
||||
ReactiveValueBlock,
|
||||
InstructionValue | ReactiveValueBlock,
|
||||
ReactiveInstruction,
|
||||
{ test: InstructionValue | null; block: ReactiveBasicBlock }
|
||||
ReactiveStatement,
|
||||
{ test: InstructionValue | null; block: ReactiveBlock }
|
||||
>
|
||||
{
|
||||
#builders: Array<Builder> = [];
|
||||
@@ -134,12 +134,12 @@ class ReactiveFunctionBuilder
|
||||
}
|
||||
appendBlock(
|
||||
block: Builder,
|
||||
item: ReactiveInstruction,
|
||||
item: ReactiveStatement,
|
||||
label?: BlockId | undefined
|
||||
): void {
|
||||
block.append(item, label);
|
||||
}
|
||||
leaveBlock(block: Builder): ReactiveBasicBlock {
|
||||
leaveBlock(block: Builder): ReactiveBlock {
|
||||
const builder = this.#builders.pop();
|
||||
invariant(
|
||||
builder === block,
|
||||
@@ -155,7 +155,7 @@ class ReactiveFunctionBuilder
|
||||
value: null,
|
||||
};
|
||||
}
|
||||
appendValueBlock(block: ReactiveValueBlock, item: ReactiveInstruction): void {
|
||||
appendValueBlock(block: ReactiveValueBlock, item: ReactiveStatement): void {
|
||||
block.instructions.push(item);
|
||||
}
|
||||
leaveValueBlock(
|
||||
@@ -175,7 +175,7 @@ class ReactiveFunctionBuilder
|
||||
enterInitBlock(block: Builder): ReactiveValueBlock {
|
||||
return this.enterValueBlock(block);
|
||||
}
|
||||
appendInitBlock(block: ReactiveValueBlock, item: ReactiveInstruction): void {
|
||||
appendInitBlock(block: ReactiveValueBlock, item: ReactiveStatement): void {
|
||||
this.appendValueBlock(block, item);
|
||||
}
|
||||
leaveInitBlock(block: ReactiveValueBlock): ReactiveValueBlock {
|
||||
@@ -191,7 +191,7 @@ class ReactiveFunctionBuilder
|
||||
visitInstruction(
|
||||
instruction: Instruction,
|
||||
value: InstructionValue | ReactiveValueBlock
|
||||
): ReactiveInstruction {
|
||||
): ReactiveStatement {
|
||||
this.visitId(instruction.id);
|
||||
const scope = getInstructionScope(instruction);
|
||||
if (scope !== null && !this.#scopes.has(scope.id)) {
|
||||
@@ -204,17 +204,17 @@ class ReactiveFunctionBuilder
|
||||
visitTerminalId(id: InstructionId): void {
|
||||
this.visitId(id);
|
||||
}
|
||||
visitImplicitTerminal(): ReactiveInstruction | null {
|
||||
visitImplicitTerminal(): ReactiveStatement | null {
|
||||
return null;
|
||||
}
|
||||
visitTerminal(
|
||||
terminal: BlockTerminal<
|
||||
ReactiveValueBlock,
|
||||
InstructionValue | ReactiveValueBlock,
|
||||
ReactiveBasicBlock,
|
||||
{ test: InstructionValue | null; block: ReactiveBasicBlock }
|
||||
ReactiveBlock,
|
||||
{ test: InstructionValue | null; block: ReactiveBlock }
|
||||
>
|
||||
): ReactiveInstruction {
|
||||
): ReactiveStatement {
|
||||
let result: ReactiveTerminal;
|
||||
switch (terminal.kind) {
|
||||
case "break": {
|
||||
@@ -259,7 +259,7 @@ class ReactiveFunctionBuilder
|
||||
test: terminal.test as Place,
|
||||
cases: terminal.cases as Array<{
|
||||
test: Place | null;
|
||||
block: ReactiveBasicBlock | void;
|
||||
block: ReactiveBlock | void;
|
||||
}>,
|
||||
};
|
||||
break;
|
||||
@@ -291,8 +291,8 @@ class ReactiveFunctionBuilder
|
||||
}
|
||||
visitCase(
|
||||
test: InstructionValue | ReactiveValueBlock | null,
|
||||
block: ReactiveBasicBlock
|
||||
): { test: InstructionValue | null; block: ReactiveBasicBlock } {
|
||||
block: ReactiveBlock
|
||||
): { test: InstructionValue | null; block: ReactiveBlock } {
|
||||
if (test !== null && test.kind !== "Identifier") {
|
||||
invariant(false, "Expected a Place");
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ import {
|
||||
Identifier,
|
||||
Instruction,
|
||||
InstructionKind,
|
||||
ReactiveBasicBlock,
|
||||
ReactiveBlock,
|
||||
ReactiveFunction,
|
||||
ReactiveScope,
|
||||
ReactiveTerminal,
|
||||
@@ -84,10 +84,7 @@ class Context {
|
||||
}
|
||||
}
|
||||
|
||||
function codegenBlock(
|
||||
cx: Context,
|
||||
block: ReactiveBasicBlock
|
||||
): t.BlockStatement {
|
||||
function codegenBlock(cx: Context, block: ReactiveBlock): t.BlockStatement {
|
||||
const statements: Array<t.Statement> = [];
|
||||
for (const item of block) {
|
||||
switch (item.kind) {
|
||||
@@ -102,7 +99,7 @@ function codegenBlock(
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "block": {
|
||||
case "scope": {
|
||||
codegenReactiveScope(cx, statements, item.scope, item.instructions);
|
||||
break;
|
||||
}
|
||||
@@ -132,7 +129,7 @@ function codegenReactiveScope(
|
||||
cx: Context,
|
||||
statements: Array<t.Statement>,
|
||||
scope: ReactiveScope,
|
||||
block: ReactiveBasicBlock
|
||||
block: ReactiveBlock
|
||||
): void {
|
||||
const cacheStoreStatements: Array<t.Statement> = [];
|
||||
const cacheLoadStatements: Array<t.Statement> = [];
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
*/
|
||||
|
||||
import { assertExhaustive } from "../Common/utils";
|
||||
import { ReactiveBasicBlock, ReactiveBlock, ReactiveFunction } from "./HIR";
|
||||
import { ReactiveBlock, ReactiveFunction, ReactiveScopeBlock } from "./HIR";
|
||||
|
||||
/**
|
||||
* Given a reactive function, flattens any scopes contained within a loop construct.
|
||||
@@ -16,12 +16,12 @@ export function flattenReactiveLoops(fn: ReactiveFunction): void {
|
||||
visit(fn.body, false);
|
||||
}
|
||||
|
||||
function visit(block: ReactiveBasicBlock, shouldFlatten: boolean): void {
|
||||
function visit(block: ReactiveBlock, shouldFlatten: boolean): void {
|
||||
let i = 0;
|
||||
while (i < block.length) {
|
||||
const item = block[i]!;
|
||||
switch (item.kind) {
|
||||
case "block": {
|
||||
case "scope": {
|
||||
if (shouldFlatten) {
|
||||
const successors = block.splice(i + 1);
|
||||
block.pop(); // remove the current element
|
||||
@@ -87,10 +87,10 @@ function visit(block: ReactiveBasicBlock, shouldFlatten: boolean): void {
|
||||
}
|
||||
}
|
||||
|
||||
function flatten(scope: ReactiveBlock, block: ReactiveBasicBlock): void {
|
||||
function flatten(scope: ReactiveScopeBlock, block: ReactiveBlock): void {
|
||||
for (const item of scope.instructions) {
|
||||
switch (item.kind) {
|
||||
case "block": {
|
||||
case "scope": {
|
||||
flatten(item, block);
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ import { invariant } from "../CompilerError";
|
||||
// *******************************************************************************************
|
||||
// *******************************************************************************************
|
||||
|
||||
// AST -> (lowering) -> HIR -> (analysis) -> Reactive Scopes -> (scheduling?) -> HIR -> (codegen) -> AST
|
||||
// AST -> (lowering) -> HIR -> (analysis) -> Reactive Scopes -> (codegen) -> AST
|
||||
|
||||
/**
|
||||
* A location in a source file, intended to be used for providing diagnostic information and
|
||||
@@ -47,27 +47,27 @@ export type ReactiveFunction = {
|
||||
params: Array<Place>;
|
||||
generator: boolean;
|
||||
async: boolean;
|
||||
body: ReactiveBasicBlock;
|
||||
body: ReactiveBlock;
|
||||
};
|
||||
|
||||
export type ReactiveBlock = {
|
||||
kind: "block";
|
||||
export type ReactiveScopeBlock = {
|
||||
kind: "scope";
|
||||
scope: ReactiveScope;
|
||||
instructions: ReactiveBasicBlock;
|
||||
instructions: ReactiveBlock;
|
||||
};
|
||||
|
||||
export type ReactiveBasicBlock = Array<ReactiveInstruction>;
|
||||
export type ReactiveBlock = Array<ReactiveStatement>;
|
||||
|
||||
export type ReactiveValueBlock = {
|
||||
kind: "value-block";
|
||||
instructions: ReactiveBasicBlock;
|
||||
instructions: ReactiveBlock;
|
||||
value: InstructionValue | null;
|
||||
};
|
||||
|
||||
export type ReactiveInstruction =
|
||||
export type ReactiveStatement =
|
||||
| { kind: "instruction"; instruction: Instruction }
|
||||
| { kind: "terminal"; terminal: ReactiveTerminal; label: BlockId | null }
|
||||
| ReactiveBlock;
|
||||
| ReactiveScopeBlock;
|
||||
|
||||
export type ReactiveTerminal =
|
||||
| { kind: "break"; label: BlockId | null }
|
||||
@@ -79,22 +79,22 @@ export type ReactiveTerminal =
|
||||
test: Place;
|
||||
cases: Array<{
|
||||
test: Place | null;
|
||||
block: ReactiveBasicBlock | void;
|
||||
block: ReactiveBlock | void;
|
||||
}>;
|
||||
}
|
||||
| { kind: "while"; test: ReactiveValueBlock; loop: ReactiveBasicBlock }
|
||||
| { kind: "while"; test: ReactiveValueBlock; loop: ReactiveBlock }
|
||||
| {
|
||||
kind: "for";
|
||||
init: ReactiveValueBlock;
|
||||
test: ReactiveValueBlock;
|
||||
update: ReactiveValueBlock;
|
||||
loop: ReactiveBasicBlock;
|
||||
loop: ReactiveBlock;
|
||||
}
|
||||
| {
|
||||
kind: "if";
|
||||
test: Place;
|
||||
consequent: ReactiveBasicBlock;
|
||||
alternate: ReactiveBasicBlock | null;
|
||||
consequent: ReactiveBlock;
|
||||
alternate: ReactiveBlock | null;
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -8,9 +8,9 @@
|
||||
import invariant from "invariant";
|
||||
import { assertExhaustive } from "../Common/utils";
|
||||
import {
|
||||
ReactiveBlock,
|
||||
ReactiveFunction,
|
||||
ReactiveInstruction,
|
||||
ReactiveScopeBlock,
|
||||
ReactiveStatement,
|
||||
ReactiveTerminal,
|
||||
ReactiveValueBlock,
|
||||
} from "./HIR";
|
||||
@@ -35,7 +35,10 @@ export function printReactiveFunction(fn: ReactiveFunction): string {
|
||||
return writer.complete();
|
||||
}
|
||||
|
||||
export function printReactiveBlock(writer: Writer, block: ReactiveBlock): void {
|
||||
export function printReactiveBlock(
|
||||
writer: Writer,
|
||||
block: ReactiveScopeBlock
|
||||
): void {
|
||||
writer.writeLine(
|
||||
`scope @${block.scope.id} [${block.scope.range.start}:${
|
||||
block.scope.range.end
|
||||
@@ -51,7 +54,7 @@ export function printReactiveBlock(writer: Writer, block: ReactiveBlock): void {
|
||||
|
||||
export function printReactiveInstructions(
|
||||
writer: Writer,
|
||||
instructions: Array<ReactiveInstruction>
|
||||
instructions: Array<ReactiveStatement>
|
||||
): void {
|
||||
writer.indented(() => {
|
||||
for (const instr of instructions) {
|
||||
@@ -62,14 +65,14 @@ export function printReactiveInstructions(
|
||||
|
||||
function printReactiveInstruction(
|
||||
writer: Writer,
|
||||
instr: ReactiveInstruction
|
||||
instr: ReactiveStatement
|
||||
): void {
|
||||
switch (instr.kind) {
|
||||
case "instruction": {
|
||||
writer.writeLine(printInstruction(instr.instruction));
|
||||
break;
|
||||
}
|
||||
case "block": {
|
||||
case "scope": {
|
||||
printReactiveBlock(writer, instr);
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ import {
|
||||
makeInstructionId,
|
||||
MutableRange,
|
||||
Place,
|
||||
ReactiveBasicBlock,
|
||||
ReactiveBlock,
|
||||
ReactiveFunction,
|
||||
ReactiveValueBlock,
|
||||
} from "./HIR";
|
||||
@@ -50,14 +50,14 @@ enum DeclKind {
|
||||
type DeclMap = Map<Identifier, { kind: DeclKind; id: InstructionId }>;
|
||||
|
||||
function visit(
|
||||
block: ReactiveBasicBlock,
|
||||
block: ReactiveBlock,
|
||||
dependencies: Set<Place>,
|
||||
declarations: DeclMap,
|
||||
scopeRange: MutableRange | null
|
||||
): void {
|
||||
for (const item of block) {
|
||||
switch (item.kind) {
|
||||
case "block": {
|
||||
case "scope": {
|
||||
const scopeDependencies: Set<Place> = new Set();
|
||||
// TODO: it would be sufficient to use a single mapping of declarations
|
||||
const scopeDeclarations: DeclMap = new Map(declarations);
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
import { assertExhaustive } from "../Common/utils";
|
||||
import {
|
||||
BlockId,
|
||||
ReactiveBasicBlock,
|
||||
ReactiveBlock,
|
||||
ReactiveFunction,
|
||||
ReactiveTerminal,
|
||||
} from "./HIR";
|
||||
@@ -23,7 +23,7 @@ export function pruneUnusedLabels(fn: ReactiveFunction): void {
|
||||
|
||||
type Labels = Set<BlockId>;
|
||||
|
||||
function visitBlock(labels: Labels, block: ReactiveBasicBlock): void {
|
||||
function visitBlock(labels: Labels, block: ReactiveBlock): void {
|
||||
for (const item of block) {
|
||||
if (item.kind === "terminal") {
|
||||
// first visit the terminal's contents, which is the only place that can
|
||||
@@ -33,7 +33,7 @@ function visitBlock(labels: Labels, block: ReactiveBasicBlock): void {
|
||||
if (item.label !== null && !labels.has(item.label)) {
|
||||
item.label = null;
|
||||
}
|
||||
} else if (item.kind === "block") {
|
||||
} else if (item.kind === "scope") {
|
||||
visitBlock(labels, item.instructions);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user