All files / TRANSPILE/3-Render/codegen/generators/expressions AccessExprGenerator.ts

100% Statements 18/18
100% Branches 8/8
100% Functions 3/3
100% Lines 18/18

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                                                49x     6x 5x         1x                       49x 5x 4x         1x                             49x           17x   11x     6x 6x       17x 17x   4x     17x       49x              
/**
 * Access Expression Generator
 *
 * Generates C code for string buffer property access:
 * - .capacity → compile-time max string length (excluding null terminator)
 * - .size → compile-time buffer size (capacity + 1, for null terminator)
 *
 * Also provides helper for bitmap field access.
 *
 * Note: Explicit length properties (.bit_length, .byte_length, .element_count,
 * .char_count) are handled in PostfixExpressionGenerator.ts, not here.
 * The deprecated .length property was removed per ADR-058.
 */
import type IBitmapFieldLayout from "../../../../../types/IBitmapFieldLayout";
import IGeneratorOutput from "../IGeneratorOutput";
import NarrowingCastHelper from "../../helpers/NarrowingCastHelper";
import invariant from "../../../../../utils/invariant";
import type TranspileState from "../../../../TranspileState";
 
/**
 * Generate code for .capacity property access.
 *
 * Only valid for string types - returns the max string length (excluding null terminator).
 */
const generateCapacityProperty = (
  capacity: number | null,
): IGeneratorOutput => {
  if (capacity !== null) {
    return { code: String(capacity), effects: [] };
  }
  // #1322: ADR-045's storage properties are E0887 in pass 2.1, decided from
  // the subject's declared type rather than from a type info this generator
  // happens to hold.
  invariant(
    false,
    `.capacity is read from a string -- E0887 rejects this in pass 2.1, before this runs`,
  );
};
 
/**
 * Generate code for .size property access.
 *
 * Only valid for string types - returns buffer size (capacity + 1 for null terminator).
 * Use with functions like fgets that need buffer size, not max length.
 */
const generateSizeProperty = (capacity: number | null): IGeneratorOutput => {
  if (capacity !== null) {
    return { code: String(capacity + 1), effects: [] };
  }
  // #1322: ADR-045's storage properties are E0887 in pass 2.1, decided from
  // the subject's declared type rather than from a type info this generator
  // happens to hold.
  invariant(
    false,
    `.size is read from a string -- E0887 rejects this in pass 2.1, before this runs`,
  );
};
 
/**
 * Generate code for bitmap field read access.
 *
 * Single bit fields generate: ((value >> offset) & 1)
 * Multi-bit fields generate: ((value >> offset) & mask)
 *
 * MISRA C:2012 Rule 10.3: When target type is known (via state.expectedType),
 * wraps expression with appropriate cast. Bool targets use != 0U comparison.
 */
const generateBitmapFieldAccess = (
  result: string,
  fieldInfo: IBitmapFieldLayout,
  state: TranspileState,
): IGeneratorOutput => {
  let expr: string;
  if (fieldInfo.width === 1) {
    // Single bit: ((value >> offset) & 1)
    expr = `((${result} >> ${fieldInfo.offset}) & 1)`;
  } else {
    // Multi-bit: ((value >> offset) & mask)
    const mask = (1 << fieldInfo.width) - 1;
    expr = `((${result} >> ${fieldInfo.offset}) & 0x${mask.toString(16).toUpperCase()})`;
  }
 
  // MISRA 10.3: Add narrowing cast if target type is known
  const targetType = state.expectedType;
  if (targetType) {
    // Bitmap operations on small types produce int in C
    expr = NarrowingCastHelper.wrap(expr, "int", targetType, state);
  }
 
  return { code: expr, effects: [] };
};
 
// Export all generators
const accessGenerators = {
  generateCapacityProperty,
  generateSizeProperty,
  generateBitmapFieldAccess,
};
 
export default accessGenerators;