All files / TRANSPILE/3-Render/codegen/helpers MemberSeparatorResolver.ts

100% Statements 24/24
100% Branches 31/31
100% Functions 4/4
100% Lines 24/24

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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167                                                                                                      254x   254x       254x         254x   254x                                   204x 3x           201x 76x       125x 20x       105x     10x         95x       19x       76x 9x       67x                       61x   61x       61x                       247x 190x           57x                  
/**
 * Member Separator Resolver
 *
 * Determines the appropriate separator for member access chains in C-Next.
 * Different separators are used based on context:
 * - `_` for scope member access (Motor.speed -> Motor_speed)
 * - `_` for register field access (GPIO7.DR_SET -> GPIO7_DR_SET)
 * - `.` for struct member access (point.x -> point.x)
 * - `->` for struct parameter member access in C mode
 * - `::` for C++ namespace/class access
 *
 * Issue #387, #409, ADR-016
 */
 
import ISeparatorContext from "../types/ISeparatorContext";
import IMemberSeparatorDeps from "../types/IMemberSeparatorDeps";
import QualifiedCName from "../../../../utils/QualifiedCName";
import QualifiedNameGenerator from "../../../../utils/QualifiedNameGenerator";
import type IRootHolding from "../types/IRootHolding";
 
/**
 * Input parameters for building a separator context
 */
interface IBuildContextInput {
  firstId: string;
  hasGlobal: boolean;
  hasThis: boolean;
  currentScopePath: string;
  /** How the root is held (`memberAccessChain.rootHolding`) */
  holding: IRootHolding;
  isCppAccess: boolean;
}
 
/**
 * Static utility for resolving member access separators
 */
class MemberSeparatorResolver {
  /**
   * Build the separator context for a member access chain
   */
  static buildContext(
    input: IBuildContextInput,
    deps: IMemberSeparatorDeps,
  ): ISeparatorContext {
    const {
      firstId,
      hasGlobal,
      hasThis,
      currentScopePath,
      holding,
      isCppAccess,
    } = input;
    const isCrossScope =
      hasGlobal &&
      (deps.isKnownScope(firstId) || deps.isKnownRegister(firstId));
 
    const scopedRegName =
      hasThis && currentScopePath
        ? QualifiedNameGenerator.forMember(currentScopePath, firstId)
        : null;
 
    const isScopedRegister =
      scopedRegName !== null && deps.isKnownRegister(scopedRegName);
 
    return {
      isCrossScope,
      holding,
      isCppAccess,
      scopedRegName,
      isScopedRegister,
    };
  }
 
  /**
   * Get the separator for the first member access operation
   */
  static getFirstSeparator(
    identifierChain: string[],
    ctx: ISeparatorContext,
    deps: IMemberSeparatorDeps,
  ): string {
    // C++ namespace/class access
    if (ctx.isCppAccess) {
      return "::";
    }
 
    // A struct parameter uses -> in C mode, . in C++ mode, and a callback-
    // promoted parameter or a local #895 made a pointer takes -> in both --
    // decided by the one helper the read path asks too (#1760 review)
    if (ctx.holding.isStructParam || ctx.holding.isPointerLocal) {
      return deps.rootMemberSeparator(ctx.holding);
    }
 
    // Cross-scope access (global.Scope.member or global.Register.member)
    if (ctx.isCrossScope) {
      return QualifiedCName.SEPARATOR;
    }
 
    // Register member access: GPIO7.DR_SET -> GPIO7_DR_SET
    if (deps.isKnownRegister(identifierChain[0])) {
      // #1322: the shadowed-register check that stood here is E0437 in pass
      // 2.1 (ADR-016); codegen only spells the C name.
      return QualifiedCName.SEPARATOR;
    }
 
    // Scope member access: Sensor.buffer -> Sensor_buffer
    // Works with or without global. prefix (both are valid syntax)
    if (deps.isKnownScope(identifierChain[0])) {
      // #1322: the cross-scope visibility check that stood here -- with its
      // #779 exemption for a scoped register -- is E0435/E0436 in pass 2.1,
      // where the same exemption is stated once for all three positions.
      return QualifiedCName.SEPARATOR;
    }
 
    // Scoped register: this.MOTOR_REG.SPEED -> Scope_MOTOR_REG_SPEED
    if (ctx.isScopedRegister) {
      return QualifiedCName.SEPARATOR;
    }
 
    // Default: struct field access
    return ".";
  }
 
  /**
   * Get the separator for subsequent member access operations (after the first)
   */
  static getSubsequentSeparator(
    identifierChain: string[],
    ctx: ISeparatorContext,
    deps: IMemberSeparatorDeps,
  ): string {
    // Check for register chains
    const chainSoFar = QualifiedCName.fromParts(identifierChain.slice(0, -1));
    const isRegisterChain =
      deps.isKnownRegister(identifierChain[0]) ||
      deps.isKnownRegister(chainSoFar) ||
      (ctx.scopedRegName !== null && deps.isKnownRegister(ctx.scopedRegName));
 
    return isRegisterChain ? QualifiedCName.SEPARATOR : ".";
  }
 
  /**
   * Get separator, dispatching to first or subsequent based on position
   */
  static getSeparator(
    isFirstOp: boolean,
    identifierChain: string[],
    ctx: ISeparatorContext,
    deps: IMemberSeparatorDeps,
  ): string {
    if (isFirstOp) {
      return MemberSeparatorResolver.getFirstSeparator(
        identifierChain,
        ctx,
        deps,
      );
    }
    return MemberSeparatorResolver.getSubsequentSeparator(
      identifierChain,
      ctx,
      deps,
    );
  }
}
 
export default MemberSeparatorResolver;