All files / utils TTypeUtils.ts

100% Statements 19/19
100% Branches 0/0
100% Functions 19/19
100% Lines 19/19

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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167                                                          9712x             1283x             78x             4x                   20x             223x             3x             2x                       134x       12x                     826x             2x             2x             2x             2x             2x             2x             2x       2x          
/**
 * Factory functions and type guards for TType.
 *
 * Provides utilities for creating and inspecting C-Next types.
 */
import type TType from "../types/TType";
import type TPrimitiveKind from "../types/TPrimitiveKind";
 
// Extract variant types from the discriminated union
type TPrimitiveType = Extract<TType, { kind: "primitive" }>;
type TStructType = Extract<TType, { kind: "struct" }>;
type TEnumType = Extract<TType, { kind: "enum" }>;
type TBitmapType = Extract<TType, { kind: "bitmap" }>;
type TArrayType = Extract<TType, { kind: "array" }>;
type TStringType = Extract<TType, { kind: "string" }>;
type TCallbackType = Extract<TType, { kind: "callback" }>;
type TRegisterType = Extract<TType, { kind: "register" }>;
type TExternalType = Extract<TType, { kind: "external" }>;
type TDeferredType = Extract<TType, { kind: "deferred" }>;
 
class TTypeUtils {
  // ============================================================================
  // Factory Functions
  // ============================================================================
 
  /**
   * Create a primitive type
   */
  static createPrimitive(primitive: TPrimitiveKind): TPrimitiveType {
    return { kind: "primitive", primitive };
  }
 
  /**
   * Create a struct type reference
   */
  static createStruct(name: string): TStructType {
    return { kind: "struct", name };
  }
 
  /**
   * Create an enum type reference
   */
  static createEnum(name: string): TEnumType {
    return { kind: "enum", name };
  }
 
  /**
   * Create a bitmap type reference
   */
  static createBitmap(name: string, bitWidth: number): TBitmapType {
    return { kind: "bitmap", name, bitWidth };
  }
 
  /**
   * Create an array type
   */
  static createArray(
    elementType: TType,
    dimensions: ReadonlyArray<number | string>,
  ): TArrayType {
    return { kind: "array", elementType, dimensions };
  }
 
  /**
   * Create a string type with capacity
   */
  static createString(capacity: number): TStringType {
    return { kind: "string", capacity };
  }
 
  /**
   * Create a callback type reference
   */
  static createCallback(name: string): TCallbackType {
    return { kind: "callback", name };
  }
 
  /**
   * Create a register type reference
   */
  static createRegister(name: string): TRegisterType {
    return { kind: "register", name };
  }
 
  /**
   * Create an external type (C++ templates, etc.)
   */
  /**
   * Create a type 1.3 Declare could not settle -- a bare name whose ADR-057
   * resolution needs the scope types this file's include closure declares, not
   * just this file's. 1.4 Resolve replaces it; nothing downstream may hold one.
   */
  static createDeferred(name: string, scopePath: string): TDeferredType {
    return { kind: "deferred", name, scopePath };
  }
 
  static createExternal(name: string): TExternalType {
    return { kind: "external", name };
  }
 
  // ============================================================================
  // Type Guards
  // ============================================================================
 
  /**
   * Check if type is a primitive
   */
  static isPrimitive(t: TType): t is TPrimitiveType {
    return t.kind === "primitive";
  }
 
  /**
   * Check if type is a struct
   */
  static isStruct(t: TType): t is TStructType {
    return t.kind === "struct";
  }
 
  /**
   * Check if type is an enum
   */
  static isEnum(t: TType): t is TEnumType {
    return t.kind === "enum";
  }
 
  /**
   * Check if type is a bitmap
   */
  static isBitmap(t: TType): t is TBitmapType {
    return t.kind === "bitmap";
  }
 
  /**
   * Check if type is an array
   */
  static isArray(t: TType): t is TArrayType {
    return t.kind === "array";
  }
 
  /**
   * Check if type is a string
   */
  static isString(t: TType): t is TStringType {
    return t.kind === "string";
  }
 
  /**
   * Check if type is a callback
   */
  static isCallback(t: TType): t is TCallbackType {
    return t.kind === "callback";
  }
 
  /**
   * Check if type is a register
   */
  static isRegister(t: TType): t is TRegisterType {
    return t.kind === "register";
  }
 
  static isExternal(t: TType): t is TExternalType {
    return t.kind === "external";
  }
}
 
export default TTypeUtils;