Home / File/ AssertConsistentIdentifiers.ts — react Source File

AssertConsistentIdentifiers.ts — react Source File

Architecture documentation for AssertConsistentIdentifiers.ts, a typescript file in the react codebase. 13 imports, 0 dependents.

File typescript MIRInfrastructure HIR 13 imports 2 functions

Entity Profile

Dependency Diagram

graph LR
  277c518d_edde_d1c4_b70b_db27c08ecf93["AssertConsistentIdentifiers.ts"]
  e96f281e_f381_272d_2359_3e6a091c9a1d["CompilerError.ts"]
  277c518d_edde_d1c4_b70b_db27c08ecf93 --> e96f281e_f381_272d_2359_3e6a091c9a1d
  e51fd0d2_bb38_cc97_7763_efe37f300a47["CompilerError"]
  277c518d_edde_d1c4_b70b_db27c08ecf93 --> e51fd0d2_bb38_cc97_7763_efe37f300a47
  18a78965_f593_105b_e5e8_07001321c2ec["HIR.ts"]
  277c518d_edde_d1c4_b70b_db27c08ecf93 --> 18a78965_f593_105b_e5e8_07001321c2ec
  9241c5c1_a9a7_17bc_e41c_e967225008dd["HIRFunction"]
  277c518d_edde_d1c4_b70b_db27c08ecf93 --> 9241c5c1_a9a7_17bc_e41c_e967225008dd
  bd003dbd_e691_524b_0cf4_50e080ffea94["Identifier"]
  277c518d_edde_d1c4_b70b_db27c08ecf93 --> bd003dbd_e691_524b_0cf4_50e080ffea94
  e3a6ca26_1f1a_c7f8_fbf3_804737192775["IdentifierId"]
  277c518d_edde_d1c4_b70b_db27c08ecf93 --> e3a6ca26_1f1a_c7f8_fbf3_804737192775
  12f5fb24_c07f_2c87_0f50_a3ec7dde7aa4["SourceLocation"]
  277c518d_edde_d1c4_b70b_db27c08ecf93 --> 12f5fb24_c07f_2c87_0f50_a3ec7dde7aa4
  6976a9ee_9d8e_4f16_3016_495f39aff2fd["PrintHIR.ts"]
  277c518d_edde_d1c4_b70b_db27c08ecf93 --> 6976a9ee_9d8e_4f16_3016_495f39aff2fd
  bf7f1cf7_fc0e_6bac_827c_8d36d98126da["printPlace"]
  277c518d_edde_d1c4_b70b_db27c08ecf93 --> bf7f1cf7_fc0e_6bac_827c_8d36d98126da
  2f3caf55_cc64_415c_55dd_9771ba7dc210["visitors.ts"]
  277c518d_edde_d1c4_b70b_db27c08ecf93 --> 2f3caf55_cc64_415c_55dd_9771ba7dc210
  10043bf1_f7ee_9ed9_307a_fe3edfd02b09["eachInstructionLValue"]
  277c518d_edde_d1c4_b70b_db27c08ecf93 --> 10043bf1_f7ee_9ed9_307a_fe3edfd02b09
  b2fc2985_a7ba_9865_c2a3_2a7531f27d44["eachInstructionValueOperand"]
  277c518d_edde_d1c4_b70b_db27c08ecf93 --> b2fc2985_a7ba_9865_c2a3_2a7531f27d44
  41232a25_deb6_6e83_05a8_ae9f961656f7["eachTerminalOperand"]
  277c518d_edde_d1c4_b70b_db27c08ecf93 --> 41232a25_deb6_6e83_05a8_ae9f961656f7
  style 277c518d_edde_d1c4_b70b_db27c08ecf93 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

/**
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

import {CompilerError} from '../CompilerError';
import {
  GeneratedSource,
  HIRFunction,
  Identifier,
  IdentifierId,
  SourceLocation,
} from './HIR';
import {printPlace} from './PrintHIR';
import {
  eachInstructionLValue,
  eachInstructionValueOperand,
  eachTerminalOperand,
} from './visitors';

/*
 * Validation pass to check that there is a 1:1 mapping between Identifier objects and IdentifierIds,
 * ie there can only be one Identifier instance per IdentifierId.
 */
export function assertConsistentIdentifiers(fn: HIRFunction): void {
  const identifiers: Identifiers = new Map();
  const assignments: Set<IdentifierId> = new Set();
  for (const [, block] of fn.body.blocks) {
    for (const phi of block.phis) {
      validate(identifiers, phi.place.identifier);
      for (const [, operand] of phi.operands) {
        validate(identifiers, operand.identifier);
      }
    }
    for (const instr of block.instructions) {
      CompilerError.invariant(instr.lvalue.identifier.name === null, {
        reason: `Expected all lvalues to be temporaries`,
        description: `Found named lvalue \`${instr.lvalue.identifier.name}\``,
        loc: instr.lvalue.loc,
      });
      CompilerError.invariant(!assignments.has(instr.lvalue.identifier.id), {
        reason: `Expected lvalues to be assigned exactly once`,
        description: `Found duplicate assignment of '${printPlace(
          instr.lvalue,
        )}'`,
        loc: instr.lvalue.loc,
      });
      assignments.add(instr.lvalue.identifier.id);
      for (const operand of eachInstructionLValue(instr)) {
        validate(identifiers, operand.identifier, operand.loc);
      }
      for (const operand of eachInstructionValueOperand(instr.value)) {
        validate(identifiers, operand.identifier, operand.loc);
      }
    }
    for (const operand of eachTerminalOperand(block.terminal)) {
      validate(identifiers, operand.identifier, operand.loc);
    }
  }
}

type Identifiers = Map<IdentifierId, Identifier>;

function validate(
  identifiers: Identifiers,
  identifier: Identifier,
  loc: SourceLocation | null = null,
): void {
  const previous = identifiers.get(identifier.id);
  if (previous === undefined) {
    identifiers.set(identifier.id, identifier);
  } else {
    CompilerError.invariant(identifier === previous, {
      reason: `Duplicate identifier object`,
      description: `Found duplicate identifier object for id ${identifier.id}`,
      loc: loc ?? GeneratedSource,
    });
  }
}

Subdomains

Types

Frequently Asked Questions

What does AssertConsistentIdentifiers.ts do?
AssertConsistentIdentifiers.ts is a source file in the react codebase, written in typescript. It belongs to the MIRInfrastructure domain, HIR subdomain.
What functions are defined in AssertConsistentIdentifiers.ts?
AssertConsistentIdentifiers.ts defines 2 function(s): assertConsistentIdentifiers, validate.
What does AssertConsistentIdentifiers.ts depend on?
AssertConsistentIdentifiers.ts imports 13 module(s): CompilerError, CompilerError.ts, HIR.ts, HIRFunction, Identifier, IdentifierId, PrintHIR.ts, SourceLocation, and 5 more.
Where is AssertConsistentIdentifiers.ts in the architecture?
AssertConsistentIdentifiers.ts is located at compiler/packages/babel-plugin-react-compiler/src/HIR/AssertConsistentIdentifiers.ts (domain: MIRInfrastructure, subdomain: HIR, directory: compiler/packages/babel-plugin-react-compiler/src/HIR).

Analyze Your Own Codebase

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

Try Supermodel Free