Home / Function/ generateInstructionTypes() — react Function Reference

generateInstructionTypes() — react Function Reference

Architecture documentation for the generateInstructionTypes() function in InferTypes.ts from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  b282c6e8_cb4e_aa17_9bb5_f36ea62991f4["generateInstructionTypes()"]
  35147ed6_ce97_e85f_570c_faf2d25f42f4["InferTypes.ts"]
  b282c6e8_cb4e_aa17_9bb5_f36ea62991f4 -->|defined in| 35147ed6_ce97_e85f_570c_faf2d25f42f4
  da9b080c_d617_2a0e_a938_61a002d496d9["generate()"]
  da9b080c_d617_2a0e_a938_61a002d496d9 -->|calls| b282c6e8_cb4e_aa17_9bb5_f36ea62991f4
  2e348290_df23_74fb_f3d0_6d6f275d86e1["equation()"]
  b282c6e8_cb4e_aa17_9bb5_f36ea62991f4 -->|calls| 2e348290_df23_74fb_f3d0_6d6f275d86e1
  22d7b6ca_548f_5459_1356_fe0992808def["setName()"]
  b282c6e8_cb4e_aa17_9bb5_f36ea62991f4 -->|calls| 22d7b6ca_548f_5459_1356_fe0992808def
  5a2f5c1a_7817_5684_6d8a_1817aafa108c["lowerType()"]
  b282c6e8_cb4e_aa17_9bb5_f36ea62991f4 -->|calls| 5a2f5c1a_7817_5684_6d8a_1817aafa108c
  319fe2c1_527d_8fac_f8b6_a96a08bc06aa["isPrimitiveBinaryOp()"]
  b282c6e8_cb4e_aa17_9bb5_f36ea62991f4 -->|calls| 319fe2c1_527d_8fac_f8b6_a96a08bc06aa
  a8cd1844_3d70_5338_2068_d0b3c257e5fa["getName()"]
  b282c6e8_cb4e_aa17_9bb5_f36ea62991f4 -->|calls| a8cd1844_3d70_5338_2068_d0b3c257e5fa
  3532df9c_cf42_5889_99aa_db6c6d3e1708["makePropertyLiteral()"]
  b282c6e8_cb4e_aa17_9bb5_f36ea62991f4 -->|calls| 3532df9c_cf42_5889_99aa_db6c6d3e1708
  da9b080c_d617_2a0e_a938_61a002d496d9["generate()"]
  b282c6e8_cb4e_aa17_9bb5_f36ea62991f4 -->|calls| da9b080c_d617_2a0e_a938_61a002d496d9
  1c006d07_fe01_836d_2c9c_015b0c23187c["unify()"]
  b282c6e8_cb4e_aa17_9bb5_f36ea62991f4 -->|calls| 1c006d07_fe01_836d_2c9c_015b0c23187c
  d7fde76c_4fd9_feb3_299b_798689f05bc6["assertExhaustive()"]
  b282c6e8_cb4e_aa17_9bb5_f36ea62991f4 -->|calls| d7fde76c_4fd9_feb3_299b_798689f05bc6
  style b282c6e8_cb4e_aa17_9bb5_f36ea62991f4 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

compiler/packages/babel-plugin-react-compiler/src/TypeInference/InferTypes.ts lines 174–575

function* generateInstructionTypes(
  env: Environment,
  names: Map<IdentifierId, string>,
  instr: Instruction,
): Generator<TypeEquation, void, undefined> {
  const {lvalue, value} = instr;
  const left = lvalue.identifier.type;

  switch (value.kind) {
    case 'TemplateLiteral':
    case 'JSXText':
    case 'Primitive': {
      yield equation(left, {kind: 'Primitive'});
      break;
    }

    case 'UnaryExpression': {
      yield equation(left, {kind: 'Primitive'});
      break;
    }

    case 'LoadLocal': {
      setName(names, lvalue.identifier.id, value.place.identifier);
      yield equation(left, value.place.identifier.type);
      break;
    }

    // We intentionally do not infer types for most context variables
    case 'DeclareContext':
    case 'LoadContext': {
      break;
    }
    case 'StoreContext': {
      /**
       * The caveat is StoreContext const, where we know the value is
       * assigned once such that everywhere the value is accessed, it
       * must have the same type from the rvalue.
       *
       * A concrete example where this is useful is `const ref = useRef()`
       * where the ref is referenced before its declaration in a function
       * expression, causing it to be converted to a const context variable.
       */
      if (value.lvalue.kind === InstructionKind.Const) {
        yield equation(
          value.lvalue.place.identifier.type,
          value.value.identifier.type,
        );
      }
      break;
    }

    case 'StoreLocal': {
      if (env.config.enableUseTypeAnnotations) {
        yield equation(
          value.lvalue.place.identifier.type,
          value.value.identifier.type,
        );
        const valueType =
          value.type === null ? makeType() : lowerType(value.type);
        yield equation(valueType, value.lvalue.place.identifier.type);
        yield equation(left, valueType);
      } else {
        yield equation(left, value.value.identifier.type);
        yield equation(
          value.lvalue.place.identifier.type,
          value.value.identifier.type,
        );
      }
      break;
    }

    case 'StoreGlobal': {
      yield equation(left, value.value.identifier.type);
      break;
    }

    case 'BinaryExpression': {
      if (isPrimitiveBinaryOp(value.operator)) {
        yield equation(value.left.identifier.type, {kind: 'Primitive'});
        yield equation(value.right.identifier.type, {kind: 'Primitive'});
      }

Subdomains

Called By

Frequently Asked Questions

What does generateInstructionTypes() do?
generateInstructionTypes() is a function in the react codebase, defined in compiler/packages/babel-plugin-react-compiler/src/TypeInference/InferTypes.ts.
Where is generateInstructionTypes() defined?
generateInstructionTypes() is defined in compiler/packages/babel-plugin-react-compiler/src/TypeInference/InferTypes.ts at line 174.
What does generateInstructionTypes() call?
generateInstructionTypes() calls 9 function(s): assertExhaustive, equation, generate, getName, isPrimitiveBinaryOp, lowerType, makePropertyLiteral, setName, and 1 more.
What calls generateInstructionTypes()?
generateInstructionTypes() is called by 1 function(s): generate.

Analyze Your Own Codebase

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

Try Supermodel Free