All files / transpiler/logic/symbols TransitiveEnumCollector.ts

100% Statements 17/17
100% Branches 4/4
100% Functions 5/5
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 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                                                                    34x 34x 49x 18x     34x                                       77x     77x 15x 15x 14x       77x                                       75x     75x 10x 10x 8x       75x          
/**
 * TransitiveEnumCollector
 * Issue #588: Extracted from Transpiler to logic layer
 *
 * Collects symbol information from transitively included .cnx files.
 * This enables proper enum prefixing when enums are defined in deeply
 * nested includes (A includes B, B includes C with enum).
 */
 
import ICodeGenSymbols from "../../types/ICodeGenSymbols";
import IncludeTreeWalker from "../../data/IncludeTreeWalker";
 
/**
 * Collects symbol information by traversing the include graph.
 *
 * When generating code, we need to know about enums defined in included files
 * so we can properly prefix enum member references. This collector walks the
 * include graph starting from a root file and gathers symbol info from all
 * transitively included .cnx files.
 */
class TransitiveEnumCollector {
  /**
   * Aggregate all known enum names from multiple symbol info sources.
   *
   * Issue #478: Enables header generation to skip forward-declaring enums
   * from included files by collecting all known enum names across all
   * processed files.
   *
   * @param symbolInfos - Iterable of ICodeGenSymbols (e.g., from symbolCollectors.values())
   * @returns Set of all known enum type names
   */
  static aggregateKnownEnums(
    symbolInfos: Iterable<ICodeGenSymbols>,
  ): Set<string> {
    const allEnums = new Set<string>();
    for (const info of symbolInfos) {
      for (const enumName of info.knownEnums) {
        allEnums.add(enumName);
      }
    }
    return allEnums;
  }
 
  /**
   * Collect symbol info from all transitively included .cnx files.
   *
   * Performs depth-first traversal of the include graph, collecting
   * ICodeGenSymbols from each visited file. Files are only visited once
   * to handle circular includes.
   *
   * @param filePath - The root file to start collecting from
   * @param symbolInfoByFile - Map of file paths to their symbol info
   * @param includeDirs - Additional directories to search for includes
   * @returns Array of ICodeGenSymbols from all transitively included .cnx files
   */
  static collect(
    filePath: string,
    symbolInfoByFile: ReadonlyMap<string, ICodeGenSymbols>,
    includeDirs: readonly string[],
  ): ICodeGenSymbols[] {
    const result: ICodeGenSymbols[] = [];
 
    // Issue #591: Use shared IncludeTreeWalker for traversal
    IncludeTreeWalker.walkFromFile(filePath, includeDirs, (file) => {
      const externalInfo = symbolInfoByFile.get(file.path);
      if (externalInfo) {
        result.push(externalInfo);
      }
    });
 
    return result;
  }
 
  /**
   * Collect symbol info for standalone mode from resolved includes.
   *
   * Issue #591: Extracted from Transpiler.transpileSource() to unify enum collection.
   * Unlike collect() which starts from a file path and parses it, this method
   * starts from already-resolved includes (from IncludeResolver.resolve()).
   *
   * @param cnextIncludes - Array of resolved C-Next include files
   * @param symbolInfoByFile - Map of file paths to their symbol info
   * @param includeDirs - Additional directories to search for nested includes
   * @returns Array of ICodeGenSymbols from all transitively included .cnx files
   */
  static collectForStandalone(
    cnextIncludes: ReadonlyArray<{ path: string }>,
    symbolInfoByFile: ReadonlyMap<string, ICodeGenSymbols>,
    includeDirs: readonly string[],
  ): ICodeGenSymbols[] {
    const result: ICodeGenSymbols[] = [];
 
    // Issue #591: Use shared IncludeTreeWalker for traversal
    IncludeTreeWalker.walk(cnextIncludes, includeDirs, (file) => {
      const externalInfo = symbolInfoByFile.get(file.path);
      if (externalInfo) {
        result.push(externalInfo);
      }
    });
 
    return result;
  }
}
 
export default TransitiveEnumCollector;