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

100% Statements 11/11
100% Branches 6/6
100% Functions 7/7
100% Lines 11/11

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                                    7x                             7x                   2x               3x                     2x             2x                                               11x   11x     11x 11x 11x                      
/**
 * Centralized error messages for CodeGenerator.
 * Extracted to improve testability of error paths.
 */
 
/**
 * Error messages and factories for code generation errors.
 */
class CodeGenErrors {
  /**
   * Error when using bracket indexing on a bitmap type.
   * Bitmap fields must be accessed by name, not index.
   */
  static bitmapBracketIndexing(
    line: number,
    bitmapTypeName: string,
    varName: string,
  ): Error {
    return new Error(
      `Error at line ${line}: Cannot use bracket indexing on bitmap type '${bitmapTypeName}'. ` +
        `Use named field access instead (e.g., ${varName}.FIELD_NAME).`,
    );
  }
 
  /**
   * Error when float bit indexing is used at global scope.
   * Float bit reads require shadow variables which need function scope.
   */
  static floatBitIndexingAtGlobalScope(
    rawName: string,
    start: string,
    width: string,
  ): Error {
    return new Error(
      `Float bit indexing reads (${rawName}[${start}, ${width}]) cannot be used at global scope. ` +
        `Move the initialization inside a function.`,
    );
  }
 
  /**
   * Error when this.Type is used outside of a scope.
   */
  static scopedTypeOutsideScope(): Error {
    return new Error("Error: 'this.Type' can only be used inside a scope");
  }
 
  /**
   * Error when sizeof is called on an array parameter.
   * C passes arrays as pointers, so sizeof would return pointer size.
   */
  static sizeofArrayParameter(varName: string): Error {
    return new Error(
      `Error: Cannot use sizeof on array parameter '${varName}'. ` +
        `Array parameters are passed as pointers in C, so sizeof would return the pointer size, not the array size. ` +
        `Pass the array size as a separate parameter instead.`,
    );
  }
 
  /**
   * Error when a required type context is missing.
   */
  static missingTypeContext(context: string): Error {
    return new Error(`Error: Missing type context in ${context}`);
  }
 
  /**
   * Error when an unsupported expression is encountered in sizeof.
   */
  static unsupportedSizeofExpression(exprText: string): Error {
    return new Error(
      `Error: Unsupported expression in sizeof: ${exprText}. ` +
        `sizeof supports types, variables, and simple expressions.`,
    );
  }
 
  /**
   * E0856 (Issue #1106): too many subscripts on a variable. Per ADR-036 each
   * subscript peels one array dimension; per ADR-007 a scalar integer/float may
   * be bit-indexed once. So a base allows at most arrayDimensions + 1
   * subscripts. Indexing further indexes a value that is not an array (e.g.
   * `flags[4][3]` on a scalar `u8` indexes the single bit `flags[4]`).
   *
   * Carries an error code so it can be looked up and appears in
   * `docs/error-codes.md` alongside the other subscript diagnostics
   * (E0850-E0852). E0853 is the precedent for a coded error raised from the
   * codegen layer rather than an analyzer.
   */
  static tooManySubscripts(
    line: number,
    varName: string,
    baseType: string,
    arrayDimensions: number,
  ): Error {
    const allowed = arrayDimensions + 1;
    const shape =
      arrayDimensions === 0
        ? `a scalar '${baseType}'`
        : `a ${arrayDimensions}-dimensional '${baseType}' array`;
    const plural = allowed === 1 ? "subscript" : "subscripts";
    const dimensionWord = arrayDimensions === 1 ? "dimension" : "dimensions";
    return new Error(
      `E0856: Error at line ${line}: too many subscripts on '${varName}'. ` +
        `'${varName}' is ${shape}, so it allows at most ${allowed} ${plural} ` +
        `(${arrayDimensions} for array ${dimensionWord} ` +
        `plus one optional bit index — ADR-036/ADR-007). Indexing further indexes a ` +
        `value that is not an array. Did you mean the bit range '${varName}[start, width]'?`,
    );
  }
}
 
export default CodeGenErrors;