All files / transpiler/output/codegen TypeRegistrationUtils.ts

100% Statements 11/11
100% Branches 10/10
100% Functions 2/2
100% Lines 11/11

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 102 103 104 105 106 107 108 109 110 111 112 113 114 115                                                                                            832x 774x     58x                     58x                             777x 769x     8x   777x   1x                         7x                       8x          
/**
 * TypeRegistrationUtils
 * Extracted helpers for enum/bitmap type registration in CodeGenerator.
 *
 * This reduces duplication across the 4 type contexts (scopedType, globalType,
 * qualifiedType, userType) that each had identical enum/bitmap handling.
 */
 
import TTypeInfo from "./types/TTypeInfo";
import TOverflowBehavior from "./types/TOverflowBehavior";
 
/**
 * Minimal symbol info interface for type registration.
 * Subset of ISymbolInfo used by registration helpers.
 */
interface ITypeSymbols {
  knownEnums: ReadonlySet<string>;
  knownBitmaps: ReadonlySet<string>;
  bitmapBitWidth: ReadonlyMap<string, number>;
}
 
/**
 * Common options for type registration.
 * Groups parameters shared by enum and bitmap registration.
 */
interface ITypeRegistrationOptions {
  name: string;
  baseType: string;
  isConst: boolean;
  overflowBehavior: TOverflowBehavior;
  isAtomic: boolean;
}
 
/**
 * Utilities for registering enum and bitmap types in the type registry.
 */
class TypeRegistrationUtils {
  /**
   * Try to register a type as an enum.
   * Returns true if the type was a known enum and was registered.
   */
  static tryRegisterEnumType(
    typeRegistry: Map<string, TTypeInfo>,
    symbols: ITypeSymbols,
    options: ITypeRegistrationOptions,
  ): boolean {
    if (!symbols.knownEnums.has(options.baseType)) {
      return false;
    }
 
    typeRegistry.set(options.name, {
      baseType: options.baseType,
      bitWidth: 0,
      isArray: false,
      isConst: options.isConst,
      isEnum: true,
      enumTypeName: options.baseType,
      overflowBehavior: options.overflowBehavior,
      isAtomic: options.isAtomic,
    });
 
    return true;
  }
 
  /**
   * Try to register a type as a bitmap.
   * Returns true if the type was a known bitmap and was registered.
   *
   * Handles both array and non-array bitmap types.
   */
  static tryRegisterBitmapType(
    typeRegistry: Map<string, TTypeInfo>,
    symbols: ITypeSymbols,
    options: ITypeRegistrationOptions,
    arrayDimensions: number[] | undefined,
  ): boolean {
    if (!symbols.knownBitmaps.has(options.baseType)) {
      return false;
    }
 
    const bitWidth = symbols.bitmapBitWidth.get(options.baseType) || 0;
 
    if (arrayDimensions && arrayDimensions.length > 0) {
      // Bitmap array
      typeRegistry.set(options.name, {
        baseType: options.baseType,
        bitWidth,
        isArray: true,
        arrayDimensions,
        isConst: options.isConst,
        isBitmap: true,
        bitmapTypeName: options.baseType,
        overflowBehavior: options.overflowBehavior,
        isAtomic: options.isAtomic,
      });
    } else {
      // Non-array bitmap
      typeRegistry.set(options.name, {
        baseType: options.baseType,
        bitWidth,
        isArray: false,
        isConst: options.isConst,
        isBitmap: true,
        bitmapTypeName: options.baseType,
        overflowBehavior: options.overflowBehavior,
        isAtomic: options.isAtomic,
      });
    }
 
    return true;
  }
}
 
export default TypeRegistrationUtils;