All files / transpiler/output/headers/generators generateBitmapHeader.ts

100% Statements 16/16
100% Branches 8/8
100% Functions 2/2
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                                        14x     14x 1x     13x 13x     13x   13x   9x 19x     9x 26x 16x   10x 10x             13x   13x        
/**
 * Bitmap Header Generator
 *
 * Generates C typedef declarations for bitmap types from symbol information.
 * Bitmaps are represented as their backing integer type with a comment
 * documenting the bit field layout.
 */
 
import IHeaderTypeInput from "./IHeaderTypeInput";
 
/**
 * Generate a C typedef declaration for the given bitmap name.
 *
 * Output format includes a comment with field layout followed by typedef.
 *
 * @param name - The bitmap type name
 * @param input - Symbol information containing bitmap fields
 * @returns C typedef with field layout comment, or simple typedef if data unavailable
 */
function generateBitmapHeader(name: string, input: IHeaderTypeInput): string {
  const backingType = input.bitmapBackingType.get(name);
 
  // Graceful fallback if bitmap data not available
  if (!backingType) {
    return `/* Bitmap: ${name} (see implementation for layout) */`;
  }
 
  const fields = input.bitmapFields.get(name);
  const lines: string[] = [];
 
  // Generate field layout comment
  lines.push(`/* Bitmap: ${name}`);
 
  if (fields && fields.size > 0) {
    // Sort fields by offset for readable layout documentation
    const sortedFields = Array.from(fields.entries()).sort(
      ([, a], [, b]) => a.offset - b.offset,
    );
 
    for (const [fieldName, { offset, width }] of sortedFields) {
      if (width === 1) {
        lines.push(` *   ${fieldName}: bit ${offset}`);
      } else {
        const endBit = offset + width - 1;
        lines.push(
          ` *   ${fieldName}: bits ${offset}-${endBit} (${width} bits)`,
        );
      }
    }
  }
 
  lines.push(" */", `typedef ${backingType} ${name};`);
 
  return lines.join("\n");
}
 
export default generateBitmapHeader;