All files / TRANSPILE/3-Render/codegen/generators/declarationGenerators RegisterBlockPlacement.ts

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

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                                                  39x           39x 4x 4x          
/**
 * #1453 / ADR-004: where a register's accessor block is emitted.
 *
 * The header when the register is part of this file's public interface, the
 * `.c` otherwise. A `#define` cannot be exported from a `.c`, so this is what
 * makes `HW.CTRL` resolve in a file that includes the one declaring `HW`.
 *
 * Asked of `PublicInterface`, which owns the rule, rather than re-derived from
 * the `public` keyword: the header and the `.c` are complements of ONE
 * decision (#1300 made the same argument for scope types), and a block
 * emitted in both files would redefine every macro.
 */
import PublicInterface from "../../../../2-Plan/PublicInterface";
import type TranspileState from "../../../../TranspileState";
 
class RegisterBlockPlacement {
  /**
   * What the `.c` should hold for this register: the block, or nothing once
   * it has been recorded for the header to print.
   *
   * @param cName - The register's transpiled C name (`GPIO7`, `Board__R`)
   * @param block - The rendered comment-and-`#define` block
   */
  static place(cName: string, block: string, state: TranspileState): string {
    const definedInHeader =
      state.sourcePath !== null &&
      PublicInterface.definesTypeInHeader(
        state.symbolTable,
        state.sourcePath,
        cName,
      );
    if (!definedInHeader) return block;
    state.exportedRegisterBlocks.push(block);
    return "";
  }
}
 
export default RegisterBlockPlacement;