Skip to content
Merged
32 changes: 29 additions & 3 deletions compiler/packages/babel-plugin-react-compiler/src/HIR/HIR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1174,11 +1174,19 @@ export type NonLocalBinding =

// Represents a user-defined variable (has a name) or a temporary variable (no name).
export type Identifier = {
/*
* unique value to distinguish a variable, since name is not guaranteed to
* exist or be unique
/**
* After EnterSSA, `id` uniquely identifies an SSA instance of a variable.
* Before EnterSSA, `id` matches `declarationId`.
*/
id: IdentifierId;

/**
* Uniquely identifies a given variable in the original program. If a value is
* reassigned in the original program each reassigned value will have a distinct
* `id` (after EnterSSA), but they will still have the same `declarationId`.
*/
declarationId: DeclarationId;

// null for temporaries. name is primarily used for debugging.
name: IdentifierName | null;
// The range for which this variable is mutable
Expand Down Expand Up @@ -1212,6 +1220,7 @@ export function makeTemporaryIdentifier(
return {
id,
name: null,
declarationId: makeDeclarationId(id),
mutableRange: {start: makeInstructionId(0), end: makeInstructionId(0)},
scope: null,
type: makeType(),
Expand Down Expand Up @@ -1508,6 +1517,23 @@ export function makeIdentifierId(id: number): IdentifierId {
return id as IdentifierId;
}

/*
* Simulated opaque type for IdentifierId to prevent using normal numbers as ids
* accidentally.
*/
const opageDeclarationId = Symbol();
export type DeclarationId = number & {[opageDeclarationId]: 'DeclarationId'};

export function makeDeclarationId(id: number): DeclarationId {
CompilerError.invariant(id >= 0 && Number.isInteger(id), {
reason: 'Expected declaration id to be a non-negative integer',
description: null,
loc: null,
suggestions: null,
});
return id as DeclarationId;
}

/*
* Simulated opaque type for InstructionId to prevent using normal numbers as ids
* accidentally.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
Terminal,
VariableBinding,
makeBlockId,
makeDeclarationId,
makeIdentifierName,
makeInstructionId,
makeTemporaryIdentifier,
Expand Down Expand Up @@ -320,6 +321,7 @@ export default class HIRBuilder {
const id = this.nextIdentifierId;
const identifier: Identifier = {
id,
declarationId: makeDeclarationId(id),
name: makeIdentifierName(name),
mutableRange: {
start: makeInstructionId(0),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import {
BasicBlock,
BlockId,
Effect,
Environment,
FunctionExpression,
GeneratedSource,
Expand All @@ -19,11 +18,14 @@ import {
LabelTerminal,
Place,
makeInstructionId,
makeType,
promoteTemporary,
reversePostorderBlocks,
} from '../HIR';
import {markInstructionIds, markPredecessors} from '../HIR/HIRBuilder';
import {
createTemporaryPlace,
markInstructionIds,
markPredecessors,
} from '../HIR/HIRBuilder';
import {eachInstructionValueOperand} from '../HIR/visitors';
import {retainWhere} from '../Utils/utils';

Expand Down Expand Up @@ -225,23 +227,7 @@ function rewriteBlock(
block.instructions.push({
id: makeInstructionId(0),
loc: terminal.loc,
lvalue: {
effect: Effect.Unknown,
identifier: {
id: env.nextIdentifierId,
mutableRange: {
start: makeInstructionId(0),
end: makeInstructionId(0),
},
name: null,
scope: null,
type: makeType(),
loc: terminal.loc,
},
kind: 'Identifier',
reactive: false,
loc: terminal.loc,
},
lvalue: createTemporaryPlace(env, terminal.loc),
value: {
kind: 'StoreLocal',
lvalue: {kind: InstructionKind.Reassign, place: {...returnValue}},
Expand All @@ -267,23 +253,7 @@ function declareTemporary(
block.instructions.push({
id: makeInstructionId(0),
loc: GeneratedSource,
lvalue: {
effect: Effect.Unknown,
identifier: {
id: env.nextIdentifierId,
mutableRange: {
start: makeInstructionId(0),
end: makeInstructionId(0),
},
name: null,
scope: null,
type: makeType(),
loc: result.loc,
},
kind: 'Identifier',
reactive: false,
loc: GeneratedSource,
},
lvalue: createTemporaryPlace(env, result.loc),
value: {
kind: 'DeclareLocal',
lvalue: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class SSABuilder {
makeId(oldId: Identifier): Identifier {
return {
id: this.nextSsaId,
declarationId: oldId.declarationId,
name: oldId.name,
mutableRange: {
start: makeInstructionId(0),
Expand Down