Home / Function/ buildReactiveScopeTerminalsHIR() — react Function Reference

buildReactiveScopeTerminalsHIR() — react Function Reference

Architecture documentation for the buildReactiveScopeTerminalsHIR() function in BuildReactiveScopeTerminalsHIR.ts from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  67879f67_c0fc_dc2d_b736_b9213153bc4a["buildReactiveScopeTerminalsHIR()"]
  779d40ec_03b1_7ad7_ef48_9ccec3c3bf70["BuildReactiveScopeTerminalsHIR.ts"]
  67879f67_c0fc_dc2d_b736_b9213153bc4a -->|defined in| 779d40ec_03b1_7ad7_ef48_9ccec3c3bf70
  170cf454_aca5_0b9b_0991_4b81319b731b["recursivelyTraverseItems()"]
  67879f67_c0fc_dc2d_b736_b9213153bc4a -->|calls| 170cf454_aca5_0b9b_0991_4b81319b731b
  ad41542d_7277_a7c4_02f9_ea27be6345d4["getScopes()"]
  67879f67_c0fc_dc2d_b736_b9213153bc4a -->|calls| ad41542d_7277_a7c4_02f9_ea27be6345d4
  f438b84b_922a_ab8c_c0d1_580131953f20["handleRewrite()"]
  67879f67_c0fc_dc2d_b736_b9213153bc4a -->|calls| f438b84b_922a_ab8c_c0d1_580131953f20
  0ed953ae_5345_6a11_8ecf_c387365fdf84["reversePostorderBlocks()"]
  67879f67_c0fc_dc2d_b736_b9213153bc4a -->|calls| 0ed953ae_5345_6a11_8ecf_c387365fdf84
  6d209f7d_6d38_4dee_c66c_76af0358f508["markPredecessors()"]
  67879f67_c0fc_dc2d_b736_b9213153bc4a -->|calls| 6d209f7d_6d38_4dee_c66c_76af0358f508
  0c09df5a_6c07_11e5_1a6b_9253007463b8["markInstructionIds()"]
  67879f67_c0fc_dc2d_b736_b9213153bc4a -->|calls| 0c09df5a_6c07_11e5_1a6b_9253007463b8
  04a5191f_97c5_f799_5b35_df80b526da7f["fixScopeAndIdentifierRanges()"]
  67879f67_c0fc_dc2d_b736_b9213153bc4a -->|calls| 04a5191f_97c5_f799_5b35_df80b526da7f
  style 67879f67_c0fc_dc2d_b736_b9213153bc4a fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

compiler/packages/babel-plugin-react-compiler/src/HIR/BuildReactiveScopeTerminalsHIR.ts lines 78–188

export function buildReactiveScopeTerminalsHIR(fn: HIRFunction): void {
  /**
   * Step 1:
   * Traverse all blocks to build up a list of rewrites. We also pre-allocate the
   * fallthrough ID here as scope start terminals and scope end terminals both
   * require a fallthrough block.
   */
  const queuedRewrites: Array<TerminalRewriteInfo> = [];
  recursivelyTraverseItems(
    [...getScopes(fn)],
    scope => scope.range,
    {
      fallthroughs: new Map(),
      rewrites: queuedRewrites,
      env: fn.env,
    },
    pushStartScopeTerminal,
    pushEndScopeTerminal,
  );

  /**
   * Step 2:
   * Traverse all blocks to apply rewrites. Here, we split blocks as described at
   * the top of this file to add scope terminals and fallthroughs.
   */
  const rewrittenFinalBlocks = new Map<BlockId, BlockId>();
  const nextBlocks = new Map<BlockId, BasicBlock>();
  /**
   * reverse queuedRewrites to pop off the end as we traverse instructions in
   * ascending order
   */
  queuedRewrites.reverse();
  for (const [, block] of fn.body.blocks) {
    const context: RewriteContext = {
      nextBlockId: block.id,
      rewrites: [],
      nextPreds: block.preds,
      instrSliceIdx: 0,
      source: block,
    };
    /**
     * Handle queued terminal rewrites at their nearest instruction ID.
     * Note that multiple terminal rewrites may map to the same instruction ID.
     */
    for (let i = 0; i < block.instructions.length + 1; i++) {
      const instrId =
        i < block.instructions.length
          ? block.instructions[i].id
          : block.terminal.id;
      let rewrite = queuedRewrites.at(-1);
      while (rewrite != null && rewrite.instrId <= instrId) {
        handleRewrite(rewrite, i, context);
        queuedRewrites.pop();
        rewrite = queuedRewrites.at(-1);
      }
    }

    if (context.rewrites.length > 0) {
      const finalBlock: BasicBlock = {
        id: context.nextBlockId,
        kind: block.kind,
        preds: context.nextPreds,
        terminal: block.terminal,
        instructions: block.instructions.slice(context.instrSliceIdx),
        phis: new Set(),
      };
      context.rewrites.push(finalBlock);
      for (const b of context.rewrites) {
        nextBlocks.set(b.id, b);
      }
      rewrittenFinalBlocks.set(block.id, finalBlock.id);
    } else {
      nextBlocks.set(block.id, block);
    }
  }
  const originalBlocks = fn.body.blocks;
  fn.body.blocks = nextBlocks;

  /**
   * Step 3:
   * Repoint phis when they refer to a rewritten block.

Subdomains

Frequently Asked Questions

What does buildReactiveScopeTerminalsHIR() do?
buildReactiveScopeTerminalsHIR() is a function in the react codebase, defined in compiler/packages/babel-plugin-react-compiler/src/HIR/BuildReactiveScopeTerminalsHIR.ts.
Where is buildReactiveScopeTerminalsHIR() defined?
buildReactiveScopeTerminalsHIR() is defined in compiler/packages/babel-plugin-react-compiler/src/HIR/BuildReactiveScopeTerminalsHIR.ts at line 78.
What does buildReactiveScopeTerminalsHIR() call?
buildReactiveScopeTerminalsHIR() calls 7 function(s): fixScopeAndIdentifierRanges, getScopes, handleRewrite, markInstructionIds, markPredecessors, recursivelyTraverseItems, reversePostorderBlocks.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free