Home / Function/ inlineImmediatelyInvokedFunctionExpressions() — react Function Reference

inlineImmediatelyInvokedFunctionExpressions() — react Function Reference

Architecture documentation for the inlineImmediatelyInvokedFunctionExpressions() function in InlineImmediatelyInvokedFunctionExpressions.ts from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  d77babff_42d3_5fdf_4059_c0b9e7cfaeb0["inlineImmediatelyInvokedFunctionExpressions()"]
  e96debd1_c62e_fa86_d259_20617c0dc175["InlineImmediatelyInvokedFunctionExpressions.ts"]
  d77babff_42d3_5fdf_4059_c0b9e7cfaeb0 -->|defined in| e96debd1_c62e_fa86_d259_20617c0dc175
  34c49c57_7996_21d5_2be2_0c39ab90eb7b["hasSingleExitReturnTerminal()"]
  d77babff_42d3_5fdf_4059_c0b9e7cfaeb0 -->|calls| 34c49c57_7996_21d5_2be2_0c39ab90eb7b
  53244187_914c_cc90_5880_7bfc1fc9c0bb["push()"]
  d77babff_42d3_5fdf_4059_c0b9e7cfaeb0 -->|calls| 53244187_914c_cc90_5880_7bfc1fc9c0bb
  d7bc118d_2cd4_19ba_9472_2ac2a9c28f4c["declareTemporary()"]
  d77babff_42d3_5fdf_4059_c0b9e7cfaeb0 -->|calls| d7bc118d_2cd4_19ba_9472_2ac2a9c28f4c
  5d6a6c3a_57cd_ff77_97d0_b01d97af89ad["rewriteBlock()"]
  d77babff_42d3_5fdf_4059_c0b9e7cfaeb0 -->|calls| 5d6a6c3a_57cd_ff77_97d0_b01d97af89ad
  b2fc2985_a7ba_9865_c2a3_2a7531f27d44["eachInstructionValueOperand()"]
  d77babff_42d3_5fdf_4059_c0b9e7cfaeb0 -->|calls| b2fc2985_a7ba_9865_c2a3_2a7531f27d44
  c447b97e_0b8e_b187_e3a8_4be412d6f495["retainWhere()"]
  d77babff_42d3_5fdf_4059_c0b9e7cfaeb0 -->|calls| c447b97e_0b8e_b187_e3a8_4be412d6f495
  0ed953ae_5345_6a11_8ecf_c387365fdf84["reversePostorderBlocks()"]
  d77babff_42d3_5fdf_4059_c0b9e7cfaeb0 -->|calls| 0ed953ae_5345_6a11_8ecf_c387365fdf84
  0c09df5a_6c07_11e5_1a6b_9253007463b8["markInstructionIds()"]
  d77babff_42d3_5fdf_4059_c0b9e7cfaeb0 -->|calls| 0c09df5a_6c07_11e5_1a6b_9253007463b8
  6d209f7d_6d38_4dee_c66c_76af0358f508["markPredecessors()"]
  d77babff_42d3_5fdf_4059_c0b9e7cfaeb0 -->|calls| 6d209f7d_6d38_4dee_c66c_76af0358f508
  style d77babff_42d3_5fdf_4059_c0b9e7cfaeb0 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

compiler/packages/babel-plugin-react-compiler/src/Inference/InlineImmediatelyInvokedFunctionExpressions.ts lines 83–262

export function inlineImmediatelyInvokedFunctionExpressions(
  fn: HIRFunction,
): void {
  // Track all function expressions that are assigned to a temporary
  const functions = new Map<IdentifierId, FunctionExpression>();
  // Functions that are inlined
  const inlinedFunctions = new Set<IdentifierId>();

  /*
   * Iterate the *existing* blocks from the outer component to find IIFEs
   * and inline them. During iteration we will modify `fn` (by inlining the CFG
   * of IIFEs) so we explicitly copy references to just the original
   * function's blocks first. As blocks are split to make room for IIFE calls,
   * the split portions of the blocks will be added to this queue.
   */
  const queue = Array.from(fn.body.blocks.values());
  queue: for (const block of queue) {
    /*
     * We can't handle labels inside expressions yet, so we don't inline IIFEs if they are in an
     * expression block.
     */
    if (isStatementBlockKind(block.kind)) {
      for (let ii = 0; ii < block.instructions.length; ii++) {
        const instr = block.instructions[ii]!;
        switch (instr.value.kind) {
          case 'FunctionExpression': {
            if (instr.lvalue.identifier.name === null) {
              functions.set(instr.lvalue.identifier.id, instr.value);
            }
            break;
          }
          case 'CallExpression': {
            if (instr.value.args.length !== 0) {
              // We don't support inlining when there are arguments
              continue;
            }
            const body = functions.get(instr.value.callee.identifier.id);
            if (body === undefined) {
              // Not invoking a local function expression, can't inline
              continue;
            }

            if (
              body.loweredFunc.func.params.length > 0 ||
              body.loweredFunc.func.async ||
              body.loweredFunc.func.generator
            ) {
              // Can't inline functions with params, or async/generator functions
              continue;
            }

            // We know this function is used for an IIFE and can prune it later
            inlinedFunctions.add(instr.value.callee.identifier.id);

            // Create a new block which will contain code following the IIFE call
            const continuationBlockId = fn.env.nextBlockId;
            const continuationBlock: BasicBlock = {
              id: continuationBlockId,
              instructions: block.instructions.slice(ii + 1),
              kind: block.kind,
              phis: new Set(),
              preds: new Set(),
              terminal: block.terminal,
            };
            fn.body.blocks.set(continuationBlockId, continuationBlock);

            /*
             * Trim the original block to contain instructions up to (but not including)
             * the IIFE
             */
            block.instructions.length = ii;

            if (hasSingleExitReturnTerminal(body.loweredFunc.func)) {
              block.terminal = {
                kind: 'goto',
                block: body.loweredFunc.func.body.entry,
                id: block.terminal.id,
                loc: block.terminal.loc,
                variant: GotoVariant.Break,
              } as GotoTerminal;
              for (const block of body.loweredFunc.func.body.blocks.values()) {

Domain

Subdomains

Frequently Asked Questions

What does inlineImmediatelyInvokedFunctionExpressions() do?
inlineImmediatelyInvokedFunctionExpressions() is a function in the react codebase, defined in compiler/packages/babel-plugin-react-compiler/src/Inference/InlineImmediatelyInvokedFunctionExpressions.ts.
Where is inlineImmediatelyInvokedFunctionExpressions() defined?
inlineImmediatelyInvokedFunctionExpressions() is defined in compiler/packages/babel-plugin-react-compiler/src/Inference/InlineImmediatelyInvokedFunctionExpressions.ts at line 83.
What does inlineImmediatelyInvokedFunctionExpressions() call?
inlineImmediatelyInvokedFunctionExpressions() calls 9 function(s): declareTemporary, eachInstructionValueOperand, hasSingleExitReturnTerminal, markInstructionIds, markPredecessors, push, retainWhere, reversePostorderBlocks, and 1 more.

Analyze Your Own Codebase

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

Try Supermodel Free