All files / transpiler/output/codegen/generators/expressions BitmapAccessHelper.ts

100% Statements 5/5
100% Branches 2/2
100% Functions 1/1
100% Lines 5/5

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                                                                                    15x 15x 7x       7x   8x              
/**
 * BitmapAccessHelper - Shared bitmap field access generation
 *
 * Deduplicates the bitmap field lookup + access pattern that was
 * previously repeated 3 times in generateMemberAccess:
 * - Primary bitmap type field access
 * - Register member bitmap type field access
 * - Struct member bitmap type field access
 */
import accessGenerators from "./AccessExprGenerator";
import TGeneratorEffect from "../TGeneratorEffect";
 
interface BitmapAccessResult {
  code: string;
  effects: readonly TGeneratorEffect[];
}
 
class BitmapAccessHelper {
  /**
   * Generate bitmap field access code.
   *
   * Looks up the field in the bitmap's field map. If found, delegates to
   * accessGenerators.generateBitmapFieldAccess. If not found, throws with
   * a descriptive error.
   *
   * @param result - The current expression result (e.g., "status", "MOTOR_CTRL")
   * @param memberName - The bitmap field name (e.g., "Running")
   * @param bitmapType - The bitmap type name (e.g., "Status", "CtrlBits")
   * @param bitmapFields - Map of bitmap type -> field map
   * @param errorDescriptor - Full descriptor for error (e.g., "type 'Status'", "register member 'X' (bitmap type 'Y')")
   * @returns Generated code and effects
   */
  static generate(
    result: string,
    memberName: string,
    bitmapType: string,
    bitmapFields: ReadonlyMap<
      string,
      ReadonlyMap<string, { readonly offset: number; readonly width: number }>
    >,
    errorDescriptor: string,
  ): BitmapAccessResult {
    const fieldInfo = bitmapFields.get(bitmapType)?.get(memberName);
    if (fieldInfo) {
      const bitmapResult = accessGenerators.generateBitmapFieldAccess(
        result,
        fieldInfo,
      );
      return { code: bitmapResult.code, effects: bitmapResult.effects };
    }
    throw new Error(
      `Error: Unknown bitmap field '${memberName}' on ${errorDescriptor}`,
    );
  }
}
 
export default BitmapAccessHelper;