All files / transpiler/output/codegen/assignment/handlers AccessPatternHandlers.ts

100% Statements 20/20
100% Branches 10/10
100% Functions 3/3
100% Lines 20/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105                                          7x     7x 3x           7x     7x                 6x 1x     5x     5x                             15x       15x   3x 1x         2x 2x 2x   2x       12x     12x               30x                
/**
 * Access pattern assignment handlers (ADR-065).
 *
 * Handles assignments with global/this prefix and member chains:
 * - GLOBAL_ARRAY: global.obj.field[i] <- value (member chain)
 * - GLOBAL_MEMBER: global.Counter.value <- 5
 * - THIS_MEMBER: this.count <- 5
 * - MEMBER_CHAIN: struct.field.subfield <- value
 */
import AssignmentKind from "../AssignmentKind";
import IAssignmentContext from "../IAssignmentContext";
import BitUtils from "../../../../../utils/BitUtils";
import TAssignmentHandler from "./TAssignmentHandler";
import CodeGenState from "../../../../state/CodeGenState";
 
/**
 * Common handler for global access patterns (GLOBAL_MEMBER and GLOBAL_ARRAY).
 *
 * Validates cross-scope visibility and generates standard assignment.
 */
function handleGlobalAccess(ctx: IAssignmentContext): string {
  const firstId = ctx.identifiers[0];
 
  // Validate cross-scope visibility if first id is a scope
  if (CodeGenState.isKnownScope(firstId) && ctx.identifiers.length >= 2) {
    CodeGenState.requireGenerator().validateCrossScopeVisibility(
      firstId,
      ctx.identifiers[1],
    );
  }
 
  const target = CodeGenState.requireGenerator().generateAssignmentTarget(
    ctx.targetCtx,
  );
  return `${target} ${ctx.cOp} ${ctx.generatedValue};`;
}
 
/**
 * Handler for `this.member <- value` (THIS_MEMBER).
 *
 * Validates scope context and generates standard assignment.
 */
function handleThisAccess(ctx: IAssignmentContext): string {
  if (!CodeGenState.currentScope) {
    throw new Error("Error: 'this' can only be used inside a scope");
  }
 
  const target = CodeGenState.requireGenerator().generateAssignmentTarget(
    ctx.targetCtx,
  );
  return `${target} ${ctx.cOp} ${ctx.generatedValue};`;
}
 
/**
 * Handle member chain: struct.field.subfield <- value
 *
 * This is the catch-all for complex member access patterns
 * that don't match more specific handlers.
 *
 * Special case: Detects bit access at the end of chain
 * (e.g., grid[2][3].flags[0] <- true) and generates RMW.
 */
function handleMemberChain(ctx: IAssignmentContext): string {
  // Check if this is bit access on a struct member
  const bitAnalysis =
    CodeGenState.requireGenerator().analyzeMemberChainForBitAccess(
      ctx.targetCtx,
    );
 
  if (bitAnalysis.isBitAccess) {
    // Validate compound operators not supported for bit access
    if (ctx.isCompound) {
      throw new Error(
        `Compound assignment operators not supported for bit field access: ${ctx.cnextOp}`,
      );
    }
 
    const { baseTarget, bitIndex, baseType } = bitAnalysis;
    const one = BitUtils.oneForType(baseType!);
    const intValue = BitUtils.boolToInt(ctx.generatedValue.trim());
 
    return `${baseTarget} = (${baseTarget} & ~(${one} << ${bitIndex})) | (${intValue} << ${bitIndex});`;
  }
 
  // Normal member chain assignment
  const target = CodeGenState.requireGenerator().generateAssignmentTarget(
    ctx.targetCtx,
  );
  return `${target} ${ctx.cOp} ${ctx.generatedValue};`;
}
 
/**
 * All access pattern handlers for registration.
 */
const accessPatternHandlers: ReadonlyArray<
  [AssignmentKind, TAssignmentHandler]
> = [
  [AssignmentKind.GLOBAL_MEMBER, handleGlobalAccess],
  [AssignmentKind.GLOBAL_ARRAY, handleGlobalAccess],
  [AssignmentKind.THIS_MEMBER, handleThisAccess],
  [AssignmentKind.MEMBER_CHAIN, handleMemberChain],
];
 
export default accessPatternHandlers;