All files / utils FunctionUtils.ts

100% Statements 7/7
100% Branches 2/2
100% Functions 4/4
100% Lines 7/7

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                                                                            26x                                                     327x 327x 83x   244x                     2x             2x          
/**
 * Factory functions and utilities for IFunctionSymbol.
 *
 * Provides utilities for creating, inspecting, and name-mangling C-Next functions.
 */
import type IFunctionSymbol from "../transpiler/types/symbols/IFunctionSymbol";
import type TVisibility from "../transpiler/types/TVisibility";
import type IScopeSymbol from "../transpiler/types/symbols/IScopeSymbol";
import type IParameterInfo from "../transpiler/types/symbols/IParameterInfo";
import type TType from "../transpiler/types/TType";
import ESourceLanguage from "./types/ESourceLanguage";
import ScopeUtils from "./ScopeUtils";
 
/**
 * Options for creating a function symbol
 */
interface IFunctionCreateOptions {
  name: string;
  scope: IScopeSymbol;
  parameters: ReadonlyArray<IParameterInfo>;
  returnType: TType;
  visibility: TVisibility;
  body: unknown;
  sourceFile: string;
  sourceLine: number;
}
 
class FunctionUtils {
  // ============================================================================
  // Factory Functions
  // ============================================================================
 
  /**
   * Create a function symbol with the given properties.
   *
   * @param options - Function properties including bare name and scope reference
   */
  static create(options: IFunctionCreateOptions): IFunctionSymbol {
    return {
      kind: "function",
      name: options.name,
      scope: options.scope,
      parameters: options.parameters,
      returnType: options.returnType,
      visibility: options.visibility,
      body: options.body,
      sourceFile: options.sourceFile,
      sourceLine: options.sourceLine,
      sourceLanguage: ESourceLanguage.CNext,
      isExported: options.visibility === "public",
    };
  }
 
  // ============================================================================
  // Transpiled C Names
  // ============================================================================
 
  /**
   * Get the transpiled C name for a function.
   *
   * For global scope functions, returns the bare name.
   * For scoped functions, returns "Scope_name" (e.g., "Test_fillData").
   * For nested scopes, returns "Outer_Inner_name".
   */
  static getTranspiledCName(func: IFunctionSymbol): string {
    const scopePath = ScopeUtils.getScopePath(func.scope);
    if (scopePath.length === 0) {
      return func.name;
    }
    return `${scopePath.join("_")}_${func.name}`;
  }
 
  // ============================================================================
  // Type Guards and Queries
  // ============================================================================
 
  /**
   * Check if function has public visibility.
   */
  static isPublic(func: IFunctionSymbol): boolean {
    return func.visibility === "public";
  }
 
  /**
   * Check if function is in the global scope.
   */
  static isInGlobalScope(func: IFunctionSymbol): boolean {
    return ScopeUtils.isGlobalScope(func.scope);
  }
}
 
export default FunctionUtils;