All files / transpiler/output/headers HeaderGenerator.ts

100% Statements 12/12
100% Branches 5/5
100% Functions 4/4
100% Lines 12/12

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                                                    195x 195x                                             140x   140x                                     2x 2x 2x 2x   2x                     3x 3x 3x          
/**
 * Header Generator (Facade)
 *
 * Delegates to CHeaderGenerator or CppHeaderGenerator based on mode.
 * Maintains backward-compatible API.
 */
 
import IHeaderSymbol from "./types/IHeaderSymbol";
import SymbolTable from "../../logic/symbols/SymbolTable";
import HeaderSymbolAdapter from "./adapters/HeaderSymbolAdapter";
import IHeaderOptions from "../codegen/types/IHeaderOptions";
import IHeaderTypeInput from "./generators/IHeaderTypeInput";
import CHeaderGenerator from "./CHeaderGenerator";
import CppHeaderGenerator from "./CppHeaderGenerator";
 
/** Pass-by-value parameter info from CodeGenerator */
type TPassByValueParams = ReadonlyMap<string, ReadonlySet<string>>;
 
/**
 * Facade that delegates header generation to the appropriate generator
 */
class HeaderGenerator {
  private readonly cGenerator: CHeaderGenerator;
  private readonly cppGenerator: CppHeaderGenerator;
 
  constructor() {
    this.cGenerator = new CHeaderGenerator();
    this.cppGenerator = new CppHeaderGenerator();
  }
 
  /**
   * Generate a header file from symbols
   *
   * @param symbols - Array of symbols to include in header
   * @param filename - Output filename (used for include guard)
   * @param options - Header generation options (includes cppMode)
   * @param typeInput - Optional type information for full definitions
   * @param passByValueParams - Map of function names to pass-by-value parameter names
   * @param allKnownEnums - All known enum names from entire compilation
   * @param sourcePath - Optional source file path for header comment
   */
  generate(
    symbols: IHeaderSymbol[],
    filename: string,
    options: IHeaderOptions = {},
    typeInput?: IHeaderTypeInput,
    passByValueParams?: TPassByValueParams,
    allKnownEnums?: ReadonlySet<string>,
    sourcePath?: string,
  ): string {
    const generator = options.cppMode ? this.cppGenerator : this.cGenerator;
 
    return generator.generate(
      symbols,
      filename,
      options,
      typeInput,
      passByValueParams,
      allKnownEnums,
      sourcePath,
    );
  }
 
  /**
   * Generate header from a symbol table, filtering by source file
   */
  generateFromSymbolTable(
    symbolTable: SymbolTable,
    sourceFile: string,
    options: IHeaderOptions = {},
  ): string {
    const tSymbols = symbolTable.getTSymbolsByFile(sourceFile);
    const headerSymbols = HeaderSymbolAdapter.fromTSymbols(tSymbols);
    const basename = sourceFile.replace(/\.[^.]+$/, "");
    const headerName = `${basename}.h`;
 
    return this.generate(headerSymbols, headerName, options);
  }
 
  /**
   * Generate header for all C-Next symbols in the symbol table
   */
  generateCNextHeader(
    symbolTable: SymbolTable,
    filename: string,
    options: IHeaderOptions = {},
  ): string {
    const tSymbols = symbolTable.getAllTSymbols();
    const headerSymbols = HeaderSymbolAdapter.fromTSymbols(tSymbols);
    return this.generate(headerSymbols, filename, options);
  }
}
 
export default HeaderGenerator;