All files / transpiler/output/codegen/helpers MemberSeparatorResolver.ts

96.66% Statements 29/30
93.93% Branches 31/33
100% Functions 4/4
96.66% Lines 29/30

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 168 169 170 171 172 173 174 175 176 177                                                                                                  190x   190x       190x     190x   190x                                         145x 3x         142x 37x     37x       105x 5x       100x   12x         12x         88x     25x 25x 25x   21x       63x 1x       62x                       34x   34x       34x                         165x 135x             30x                  
/**
 * 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";
 
/**
 * Input parameters for building a separator context
 */
interface IBuildContextInput {
  firstId: string;
  hasGlobal: boolean;
  hasThis: boolean;
  currentScope: string | null;
  isStructParam: boolean;
  isCppAccess: boolean;
  forcePointerSemantics?: 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,
      currentScope,
      isStructParam,
      isCppAccess,
      forcePointerSemantics,
    } = input;
    const isCrossScope =
      hasGlobal &&
      (deps.isKnownScope(firstId) || deps.isKnownRegister(firstId));
 
    const scopedRegName =
      hasThis && currentScope ? `${currentScope}_${firstId}` : null;
 
    const isScopedRegister =
      scopedRegName !== null && deps.isKnownRegister(scopedRegName);
 
    return {
      hasGlobal,
      isCrossScope,
      isStructParam,
      isCppAccess,
      scopedRegName,
      isScopedRegister,
      forcePointerSemantics,
    };
  }
 
  /**
   * Get the separator for the first member access operation
   */
  static getFirstSeparator(
    identifierChain: string[],
    memberName: string,
    ctx: ISeparatorContext,
    deps: IMemberSeparatorDeps,
  ): string {
    // C++ namespace/class access
    if (ctx.isCppAccess) {
      return "::";
    }
 
    // Struct parameter uses -> in C mode, . in C++ mode
    // Issue #895: forcePointerSemantics overrides C++ mode to use ->
    if (ctx.isStructParam) {
      Iif (ctx.forcePointerSemantics) {
        return "->";
      }
      return deps.getStructParamSeparator();
    }
 
    // Cross-scope access (global.Scope.member or global.Register.member)
    if (ctx.isCrossScope) {
      return "_";
    }
 
    // Register member access: GPIO7.DR_SET -> GPIO7_DR_SET
    if (deps.isKnownRegister(identifierChain[0])) {
      // Validate register access from inside scope requires global. prefix
      deps.validateRegisterAccess(
        identifierChain[0],
        memberName,
        ctx.hasGlobal,
      );
      return "_";
    }
 
    // Scope member access: Sensor.buffer -> Sensor_buffer
    // Works with or without global. prefix (both are valid syntax)
    if (deps.isKnownScope(identifierChain[0])) {
      // Issue #779: Skip cross-scope validation for scoped register access
      // Board.GPIO where Board_GPIO is a known register is valid
      const scopedRegisterName = `${identifierChain[0]}_${memberName}`;
      Eif (!deps.isKnownRegister(scopedRegisterName)) {
        deps.validateCrossScopeVisibility(identifierChain[0], memberName);
      }
      return "_";
    }
 
    // Scoped register: this.MOTOR_REG.SPEED -> Scope_MOTOR_REG_SPEED
    if (ctx.isScopedRegister) {
      return "_";
    }
 
    // 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 = identifierChain.slice(0, -1).join("_");
    const isRegisterChain =
      deps.isKnownRegister(identifierChain[0]) ||
      deps.isKnownRegister(chainSoFar) ||
      (ctx.scopedRegName !== null && deps.isKnownRegister(ctx.scopedRegName));
 
    return isRegisterChain ? "_" : ".";
  }
 
  /**
   * Get separator, dispatching to first or subsequent based on position
   */
  static getSeparator(
    isFirstOp: boolean,
    identifierChain: string[],
    memberName: string,
    ctx: ISeparatorContext,
    deps: IMemberSeparatorDeps,
  ): string {
    if (isFirstOp) {
      return MemberSeparatorResolver.getFirstSeparator(
        identifierChain,
        memberName,
        ctx,
        deps,
      );
    }
    return MemberSeparatorResolver.getSubsequentSeparator(
      identifierChain,
      ctx,
      deps,
    );
  }
}
 
export default MemberSeparatorResolver;