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

100% Statements 20/20
100% Branches 14/14
100% Functions 2/2
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                                                    408x 303x       105x 105x 28x 28x       77x 7x 7x       70x 70x 4x 4x 4x       66x                 338x 240x       98x 21x     77x        
/**
 * C-Next to C Type Mapping Utility
 *
 * Provides shared type mapping functionality for both header
 * and implementation code generation.
 *
 * Source of truth: src/constants/TypeMappings.ts
 */
 
import CNEXT_TO_C_TYPE_MAP from "../../../../utils/constants/TypeMappings";
 
/**
 * Map a C-Next type to C type
 *
 * Handles:
 * - Direct primitive type mappings (u32 -> uint32_t)
 * - Pointer types (u32* -> uint32_t*)
 * - Array types (u32[10] -> uint32_t[10])
 * - String types (string<N> -> char[N+1])
 * - User-defined types (pass through unchanged)
 *
 * @param type - The C-Next type string
 * @returns The corresponding C type string
 */
function mapType(type: string): string {
  // Check direct mapping first
  if (CNEXT_TO_C_TYPE_MAP[type]) {
    return CNEXT_TO_C_TYPE_MAP[type];
  }
 
  // Issue #427: Handle string<N> types -> char[N+1]
  const stringMatch = /^string<(\d+)>$/.exec(type);
  if (stringMatch) {
    const capacity = Number.parseInt(stringMatch[1], 10);
    return `char[${capacity + 1}]`;
  }
 
  // Handle pointer types
  if (type.endsWith("*")) {
    const baseType = type.slice(0, -1).trim();
    return `${mapType(baseType)}*`;
  }
 
  // Handle array types (simplified)
  const arrayMatch = /^(\w+)\[(\d*)\]$/.exec(type);
  if (arrayMatch) {
    const baseType = mapType(arrayMatch[1]);
    const size = arrayMatch[2] || "";
    return `${baseType}[${size}]`;
  }
 
  // User-defined types pass through
  return type;
}
 
/**
 * Check if a type is a built-in C-Next type (primitive or string<N>)
 * Used by header generator to avoid generating forward declarations for built-in types
 */
function isBuiltInType(typeName: string): boolean {
  // Direct primitive types
  if (CNEXT_TO_C_TYPE_MAP[typeName]) {
    return true;
  }
 
  // Issue #427: string<N> is a built-in type
  if (/^string<\d+>$/.test(typeName)) {
    return true;
  }
 
  return false;
}
 
export default { TYPE_MAP: CNEXT_TO_C_TYPE_MAP, mapType, isBuiltInType };