All files / transpiler/logic/symbols/cpp/collectors EnumCollector.ts

89.47% Statements 17/19
57.14% Branches 8/14
100% Functions 1/1
100% Lines 17/17

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                                                              8x 8x   8x 8x   8x 8x       8x 8x 8x 7x 7x 7x 7x 7x 7x 7x           8x                            
/**
 * EnumCollector - Extracts enum declarations from C++ parse trees.
 *
 * Produces ICppEnumSymbol instances with optional bit width for typed enums.
 */
 
/* eslint-disable @typescript-eslint/no-explicit-any */
 
import ESourceLanguage from "../../../../../utils/types/ESourceLanguage";
import ICppEnumSymbol from "../../../../types/symbols/cpp/ICppEnumSymbol";
import SymbolTable from "../../SymbolTable";
import SymbolUtils from "../../SymbolUtils";
 
class EnumCollector {
  /**
   * Collect an enum specifier and return an ICppEnumSymbol.
   *
   * @param enumSpec The enum specifier context
   * @param sourceFile Source file path
   * @param line Line number
   * @param currentNamespace Optional current namespace
   * @param symbolTable Optional symbol table for storing bit width
   * @returns The enum symbol or null if no name
   */
  static collect(
    enumSpec: any,
    sourceFile: string,
    line: number,
    currentNamespace?: string,
    symbolTable?: SymbolTable | null,
  ): ICppEnumSymbol | null {
    const enumHead = enumSpec.enumHead?.();
    Iif (!enumHead) return null;
 
    const identifier = enumHead.Identifier?.();
    Iif (!identifier) return null;
 
    const name = identifier.getText();
    const fullName = currentNamespace ? `${currentNamespace}::${name}` : name;
 
    // Extract bit width for typed enums (e.g., enum EPressureType : uint8_t)
    let bitWidth: number | undefined;
    Eif (symbolTable) {
      const enumbase = enumHead.enumbase?.();
      if (enumbase) {
        const typeSpecSeq = enumbase.typeSpecifierSeq?.();
        Eif (typeSpecSeq) {
          const typeName = typeSpecSeq.getText();
          const width = SymbolUtils.getTypeWidth(typeName);
          Eif (width > 0) {
            symbolTable.addEnumBitWidth(fullName, width);
            bitWidth = width;
          }
        }
      }
    }
 
    return {
      kind: "enum",
      name: fullName,
      sourceFile,
      sourceLine: line,
      sourceLanguage: ESourceLanguage.Cpp,
      isExported: true,
      parent: currentNamespace,
      bitWidth,
    };
  }
}
 
export default EnumCollector;