All files / transpiler/output/codegen/generators/declarationGenerators BitmapGenerator.ts

100% Statements 16/16
100% Branches 6/6
100% Functions 1/1
100% Lines 16/16

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                                                                13x           19x   19x     19x 19x     19x 19x 1x       18x   18x     18x     18x 19x 13x     18x   18x              
/**
 * BitmapGenerator - ADR-034 Bitmap Declaration Generation
 *
 * Generates C typedef declarations from C-Next bitmap syntax.
 * Bitmaps are fixed-width integers with named bit fields.
 *
 * Example:
 *   bitmap8 MotorFlags { Running, Direction, Mode[3], Reserved[2] }
 *   ->
 *   // Bitmap: MotorFlags
 *   // Fields:
 *   //   Running: bit 0 (1 bit)
 *   //   Direction: bit 1 (1 bit)
 *   //   Mode: bits 2-4 (3 bits)
 *   //   Reserved: bits 5-6 (2 bits)
 *   typedef uint8_t MotorFlags;
 */
import * as Parser from "../../../../logic/parser/grammar/CNextParser";
import IGeneratorInput from "../IGeneratorInput";
import IGeneratorState from "../IGeneratorState";
import IGeneratorOutput from "../IGeneratorOutput";
import IOrchestrator from "../IOrchestrator";
import TGeneratorFn from "../TGeneratorFn";
import TGeneratorEffect from "../TGeneratorEffect";
import BitmapCommentUtils from "./BitmapCommentUtils";
 
/**
 * Generate a C typedef from a C-Next bitmap declaration.
 *
 * ADR-034: Bitmaps provide type-safe bit field access.
 * The backing type (uint8_t, uint16_t, uint32_t) depends on the bitmap size.
 */
const generateBitmap: TGeneratorFn<Parser.BitmapDeclarationContext> = (
  node: Parser.BitmapDeclarationContext,
  input: IGeneratorInput,
  state: IGeneratorState,
  _orchestrator: IOrchestrator,
): IGeneratorOutput => {
  const effects: TGeneratorEffect[] = [];
 
  const name = node.IDENTIFIER().getText();
 
  // ADR-016: Apply scope prefix if inside a scope
  const prefix = state.currentScope ? `${state.currentScope}_` : "";
  const fullName = `${prefix}${name}`;
 
  // Look up backing type from symbols (collected by SymbolCollector)
  const backingType = input.symbols?.bitmapBackingType.get(fullName);
  if (!backingType) {
    throw new Error(`Error: Bitmap ${fullName} not found in registry`);
  }
 
  // Bitmap requires stdint.h for uint8_t, uint16_t, etc.
  effects.push({ type: "include", header: "stdint" });
 
  const lines: string[] = [];
 
  // Generate comment with field layout
  lines.push(`/* Bitmap: ${fullName} */`);
 
  // Issue #707: Use shared utility for bitmap field comments
  const fields = input.symbols?.bitmapFields.get(fullName);
  if (fields) {
    lines.push(...BitmapCommentUtils.generateBitmapFieldComments(fields));
  }
 
  lines.push(`typedef ${backingType} ${fullName};`, "");
 
  return {
    code: lines.join("\n"),
    effects,
  };
};
 
export default generateBitmap;