All files / utils ArrayDimensionParser.ts

96.25% Statements 77/80
87.93% Branches 51/58
78.57% Functions 11/14
96.25% Lines 77/80

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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341                                                                                                      45x                   45x   45x 45x 45x   45x   45x   45x                                       730x     730x 730x 643x       87x 87x 27x       60x 60x 7x       53x                   87x 87x 49x   38x                               60x 7x             60x 219x 219x 208x   11x 11x 11x 7x 7x     53x                             22x 22x 10x   12x                     53x 53x 11x       42x 42x 7x       35x 35x 2x 2x 2x 2x         33x 33x 2x 2x 2x 2x       31x                             7x 7x                                 1139x 1124x                   15x 15x 19x 19x     19x     15x                           62x 62x 47x     15x 20x   15x                                               62x               20x     20x              
/**
 * ArrayDimensionParser - the single evaluator for array dimension expressions.
 *
 * Issue #644 extracted it from CodeGenerator; issue #1127 made it the only
 * place a dimension is decided. It lives in utils/ rather than output/ so the
 * symbol layer can reach it too -- `logic/` cannot import `output/`, and
 * collection folding fewer forms than codegen is what let the .c and the .h
 * disagree.
 *
 * Three rules hold across every entry point:
 *
 * 1. One evaluator. `parseSingleDimension` decides what an expression is
 *    worth; the other methods differ only in the shape of input they take
 *    (one expression, a dimension list) -- never in the answer.
 * 2. One lookup set. Callers pass `IConstantEvalOptions`, built by
 *    `dimensionEvalOptions()` in codegen and from the collection-time const
 *    map in the symbol layer. A caller that supplies fewer resolves fewer
 *    forms than its peers, which is a divergence waiting to happen.
 * 3. An unresolved dimension keeps its slot, as `UNRESOLVED_DIMENSION`.
 *    Dropping one shifts every dimension after it, so a subscript gets
 *    validated against the wrong bound.
 *
 * The string-level counterpart is `ArrayDimensionText`, for C and C++
 * declarators that arrive as text with no parse tree behind them.
 */
 
import LiteralUtils from "./LiteralUtils.js";
import * as Parser from "../transpiler/logic/parser/grammar/CNextParser.js";
import UNRESOLVED_DIMENSION from "../transpiler/constants/UNRESOLVED_DIMENSION.js";
 
/**
 * Options for evaluating constant expressions.
 */
interface IConstantEvalOptions {
  /** Map of const variable names to their numeric values */
  constValues?: Map<string, number>;
  /** Map of type names to their bit widths (for sizeof) */
  typeWidths?: Record<string, number>;
}
 
/**
 * Helper class for parsing array dimension expressions.
 *
 * Supports various expression forms:
 * - Integer literals (decimal, hex, binary)
 * - Const variable references
 * - sizeof(type) expressions
 * - Binary expressions with const values (CONST + CONST)
 */
class ArrayDimensionParser {
  /** Regex for identifier pattern */
  private static readonly IDENTIFIER_RE = /^[a-zA-Z_]\w*$/;
  /**
   * Regex for addition of two operands, each an integer literal or a const
   * identifier: `8+1`, `SIZE+1`, `1+SIZE`, `SIZE+OFFSET`.
   *
   * Issue #1157: this required an identifier on both sides, so `u8[8+1]` did
   * not fold. A dimension that does not fold is dropped by the collectors, which left
   * the field marked as an array with no dimensions -- the header emitted a
   * scalar and the body fell back to bit indexing.
   */
  private static readonly ADD_RE = /^(\w+)\+(\w+)$/;
  /** Same shape as ADD_RE for subtraction and multiplication. */
  private static readonly SUBTRACT_RE = /^(\w+)-(\w+)$/;
  private static readonly MULTIPLY_RE = /^(\w+)\*(\w+)$/;
  private static readonly DIVIDE_RE = /^(\w+)\/(\w+)$/;
  /** Regex for sizeof(type) */
  private static readonly SIZEOF_RE = /^sizeof\(([a-zA-Z_]\w*)\)$/;
  /** Regex for sizeof(type) * N */
  private static readonly SIZEOF_MUL_RE = /^sizeof\(([a-zA-Z_]\w*)\)\*(\d+)$/;
  /** Regex for sizeof(type) + N */
  private static readonly SIZEOF_ADD_RE = /^sizeof\(([a-zA-Z_]\w*)\)\+(\d+)$/;
 
  /**
   * Parse a single expression as a compile-time constant.
   *
   * This is the most complete evaluation, supporting:
   * - Integer literals (decimal, hex, binary)
   * - Const variable references
   * - sizeof(type) for primitive types
   * - sizeof(type) * N and sizeof(type) + N
   * - Binary expressions with const values (CONST + CONST)
   *
   * @param expr - The expression context to evaluate
   * @param options - Optional evaluation options
   * @returns The numeric value if constant, undefined if not evaluable
   */
  static parseSingleDimension(
    expr: Parser.ExpressionContext,
    options?: IConstantEvalOptions,
  ): number | undefined {
    const text = expr.getText().trim();
 
    // Try integer literal first (most common case)
    const literalValue = LiteralUtils.parseIntegerLiteral(text);
    if (literalValue !== undefined) {
      return literalValue;
    }
 
    // Try const identifier lookup
    const constResult = this._tryResolveConstIdentifier(text, options);
    if (constResult !== undefined) {
      return constResult;
    }
 
    // Try const binary expression (CONST + CONST)
    const binaryResult = this._tryEvaluateConstBinaryExpr(text, options);
    if (binaryResult !== undefined) {
      return binaryResult;
    }
 
    // Try sizeof expressions
    return this._tryEvaluateSizeofExpr(text, options);
  }
 
  /**
   * Try to resolve text as a const identifier.
   */
  private static _tryResolveConstIdentifier(
    text: string,
    options?: IConstantEvalOptions,
  ): number | undefined {
    const constValues = options?.constValues;
    if (!constValues || !this.IDENTIFIER_RE.test(text)) {
      return undefined;
    }
    return constValues.get(text);
  }
 
  /**
   * Try to evaluate text as a const binary expression (CONST + CONST).
   */
  private static _tryEvaluateConstBinaryExpr(
    text: string,
    options?: IConstantEvalOptions,
  ): number | undefined {
    // Issue #1127: `+` alone was not enough. A dimension that does not fold is
    // carried as source text, and source text in a header is a C-Next name
    // that does not exist in C -- `u8[LOCAL*2]` reached the .h as
    // `uint8_t localMul[LOCAL*2]`, which fails to compile. Folding the common
    // const arithmetic removes the cases that actually occur; the residual
    // category is tracked separately.
    const operators: [RegExp, (a: number, b: number) => number][] = [
      [this.ADD_RE, (a, b) => a + b],
      [this.SUBTRACT_RE, (a, b) => a - b],
      [this.MULTIPLY_RE, (a, b) => a * b],
      // Integer division, matching C semantics for an array bound.
      [this.DIVIDE_RE, (a, b) => (b === 0 ? Number.NaN : Math.trunc(a / b))],
    ];
 
    for (const [pattern, apply] of operators) {
      const match = pattern.exec(text);
      if (!match) {
        continue;
      }
      const left = this._resolveOperand(match[1], options);
      const right = this._resolveOperand(match[2], options);
      if (left !== undefined && right !== undefined) {
        const value = apply(left, right);
        return Number.isNaN(value) ? undefined : value;
      }
    }
    return undefined;
  }
 
  /**
   * Resolve one operand of a constant expression.
   *
   * An operand is either an integer literal in any notation that
   * `LiteralUtils.parseIntegerLiteral` accepts (decimal, hex, binary) or the
   * name of a known const. Resolving both operand kinds in one place is what
   * lets `8+1`, `SIZE+1` and `SIZE+OFFSET` fold by the same rule.
   */
  private static _resolveOperand(
    text: string,
    options?: IConstantEvalOptions,
  ): number | undefined {
    const literal = LiteralUtils.parseIntegerLiteral(text);
    if (literal !== undefined) {
      return literal;
    }
    return options?.constValues?.get(text);
  }
 
  /**
   * Try to evaluate text as a sizeof expression.
   * Handles: sizeof(type), sizeof(type) * N, sizeof(type) + N
   */
  private static _tryEvaluateSizeofExpr(
    text: string,
    options?: IConstantEvalOptions,
  ): number | undefined {
    const typeWidths = options?.typeWidths;
    if (!typeWidths) {
      return undefined;
    }
 
    // Try sizeof(type)
    const sizeofMatch = this.SIZEOF_RE.exec(text);
    if (sizeofMatch) {
      return this._evaluateSimpleSizeof(sizeofMatch[1], typeWidths);
    }
 
    // Try sizeof(type) * N
    const mulMatch = this.SIZEOF_MUL_RE.exec(text);
    if (mulMatch) {
      const bitWidth = typeWidths[mulMatch[1]];
      const multiplier = Number.parseInt(mulMatch[2], 10);
      Eif (bitWidth && !Number.isNaN(multiplier)) {
        return (bitWidth / 8) * multiplier;
      }
    }
 
    // Try sizeof(type) + N
    const addMatch = this.SIZEOF_ADD_RE.exec(text);
    if (addMatch) {
      const bitWidth = typeWidths[addMatch[1]];
      const addend = Number.parseInt(addMatch[2], 10);
      Eif (bitWidth && !Number.isNaN(addend)) {
        return bitWidth / 8 + addend;
      }
    }
 
    return undefined;
  }
 
  /**
   * Evaluate simple sizeof(type) expression.
   */
  private static _evaluateSimpleSizeof(
    typeName: string,
    typeWidths: Record<string, number>,
  ): number | undefined {
    // Issue #1127: an isKnownStruct predicate used to be threaded in here to
    // distinguish "known struct, size not computable yet" from "unknown type".
    // Both returned undefined, so it changed no answer -- but it meant callers
    // supplied different option sets and agreed only because the difference was
    // inert, which is the divergence dimensionEvalOptions exists to prevent.
    const bitWidth = typeWidths[typeName];
    return bitWidth ? bitWidth / 8 : undefined;
  }
 
  /**
   * Parse all array dimensions, dropping any that can't be resolved.
   *
   * Used for bitmap array registration and other contexts where
   * unresolved dimensions should be skipped.
   *
   * @param arrayDims - The array dimension contexts to parse
   * @param options - Optional evaluation options
   * @returns Array of resolved dimension values, or undefined if none resolved
   */
  static parseAllDimensions(
    arrayDims: Parser.ArrayDimensionContext[] | null,
    options?: IConstantEvalOptions,
  ): number[] | undefined {
    if (!arrayDims || arrayDims.length === 0) {
      return undefined;
    }
 
    // Issue #1127: keep the slot, the same policy parseDimensions applies.
    // This filtered `size > 0` and so dropped an unresolved dimension, which
    // shifts every dimension after it -- `u8[2] x[EColor.COUNT]` recorded [2]
    // here and [2, UNRESOLVED_DIMENSION] through the sibling loop 15 lines
    // away that shares this evaluator. Inert today only because trailing
    // C-style dimensions are rejected later, which is agreement by
    // coincidence rather than one policy.
    const dimensions: number[] = [];
    for (const dim of arrayDims) {
      const sizeExpr = dim.expression();
      const size = sizeExpr
        ? ArrayDimensionParser.parseSingleDimension(sizeExpr, options)
        : undefined;
      dimensions.push(size ?? UNRESOLVED_DIMENSION);
    }
 
    return dimensions.length > 0 ? dimensions : undefined;
  }
 
  /**
   * Iterate array dimensions with a callback for processing each.
   * Handles null/empty array check centrally.
   */
  private static forEachDimension(
    arrayDims: Parser.ArrayDimensionContext[] | null,
    processDim: (
      sizeExpr: Parser.ExpressionContext | null,
      dimensions: number[],
    ) => void,
  ): number[] {
    const dimensions: number[] = [];
    if (!arrayDims || arrayDims.length === 0) {
      return dimensions;
    }
 
    for (const dim of arrayDims) {
      processDim(dim.expression(), dimensions);
    }
    return dimensions;
  }
 
  /**
   * Parse array dimensions, keeping one entry per dimension.
   *
   * Issue #1127: this replaces `parseSimpleDimensions` and
   * `parseForParameters`, which differed only in what they did with a
   * dimension that did not resolve -- one omitted it, the other recorded 0.
   * Omitting shifts every dimension after it out of position, so a subscript
   * gets validated against the wrong bound; that is the failure
   * `UNRESOLVED_DIMENSION` exists to prevent, and there is no context in which
   * omitting is the better answer. Two near-identical bodies with divergent
   * policies is also exactly the duplicate path this work set out to remove.
   *
   * @param arrayDims - The array dimension contexts to parse
   * @returns One entry per dimension: the literal value, or
   *          `UNRESOLVED_DIMENSION` when the size is not a literal (a const
   *          identifier, an expression) or the dimension is unsized (`arr[]`)
   */
  static parseDimensions(
    arrayDims: Parser.ArrayDimensionContext[] | null,
    options?: IConstantEvalOptions,
  ): number[] {
    return ArrayDimensionParser.forEachDimension(
      arrayDims,
      (sizeExpr, dimensions) => {
        // Issue #1127: takes the same options every other entry point takes.
        // Without them this was the one entry point that structurally could
        // not reach constValues or typeWidths, so `u8 buf[SIZE]` recorded
        // UNRESOLVED_DIMENSION while `u8[SIZE] buf` -- the other branch of the
        // very same function -- recorded 6.
        const size = sizeExpr
          ? ArrayDimensionParser.parseSingleDimension(sizeExpr, options)
          : undefined;
        dimensions.push(size ?? UNRESOLVED_DIMENSION);
      },
    );
  }
}
 
export default ArrayDimensionParser;