All files / PARSE/3-Declare/cnext/collectors VariableCollector.ts

100% Statements 64/64
100% Branches 57/57
100% Functions 11/11
100% Lines 61/61

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                                                                                        4988x 4978x     10x 4988x 3x     7x                     27x       27x 26x       1x                         28x                       23x   23x 27x     23x                     459x 459x 514x   514x   27x       27x                 487x   459x                                                           4988x       4988x 4988x               4988x       4988x 4988x         4988x         4988x 4988x 4988x 4988x 4988x 4988x           4988x   4988x     4988x 459x                 4988x 23x           4988x 4988x 4988x   4988x                                                       4988x     4988x 3191x 3191x 3191x 46x 46x       3145x 3145x           502x                                             2218x 2218x 2218x     2218x                                                       2218x          
/**
 * VariableCollector - Extracts variable declarations from parse trees.
 * Handles types, const modifier, arrays, and initial values.
 *
 * Produces TType-based IVariableSymbol with proper IScopeSymbol references.
 */
 
import * as Parser from "../../../2-Parse/grammar/CNextParser";
import DimensionResolver from "../utils/DimensionResolver";
import ESourceLanguage from "../../../../utils/types/ESourceLanguage";
import IVariableSymbol from "../../../../types/symbols/IVariableSymbol";
import ArrayInitializerUtils from "../utils/ArrayInitializerUtils";
import TypeUtils from "../utils/TypeUtils";
import StringUtils from "../../../../utils/StringUtils";
import TTypeUtils from "../../../../utils/TTypeUtils";
import type TType from "../../../../types/TType";
import type TOverflowBehavior from "../../../../types/TOverflowBehavior";
import ScopeUtils from "../../../../utils/ScopeUtils";
import TVisibility from "../../../../types/TVisibility";
import OverflowBehaviorUtils from "../../../../utils/OverflowBehaviorUtils";
import ParserUtils from "../../../../utils/ParserUtils";
import ExpressionUnwrapper from "../../../../utils/ExpressionUnwrapper";
import ConstExprLowering from "../../../../utils/ConstExprLowering";
import type TConstExpr from "../../../../types/TConstExpr";
import type IDeclaredDimension from "../types/IDeclaredDimension";
 
class VariableCollector {
  /**
   * Resolve a variable's declared type.
   *
   * ADR-045: a bare `string` takes its capacity from the initializing literal.
   * TypeResolver cannot do this on the type string alone — bare `string` matches
   * no pattern there and falls through to a *struct* named "string", which the
   * header then emits verbatim (`extern const string VERSION;`). The `.c` path
   * inferred the capacity independently, so the two disagreed silently until
   * the `.c` began including its own header (#1164).
   *
   * The inference rule itself is StringUtils.literalLength, shared with codegen.
   */
  private static resolveDeclaredType(
    typeStr: string,
    ctx: Parser.VariableDeclarationContext | Parser.ForVarDeclContext,
    resolved: TType,
  ): TType {
    if (typeStr !== "string") {
      return resolved;
    }
 
    const initText = ctx.expression()?.getText() ?? "";
    if (!initText.startsWith('"') || !initText.endsWith('"')) {
      return resolved;
    }
 
    return TTypeUtils.createString(StringUtils.literalLength(initText));
  }
 
  /**
   * Resolve a single array dimension to a number or string ("" when it is
   * omitted and there is nothing to count it from).
   */
  private static resolveDimension(
    dim: Parser.ArrayDimensionContext,
    initExpr: Parser.ExpressionContext | null,
  ): IDeclaredDimension {
    const sizeExpr = dim.expression();
 
    // A literal folds here; a const or a C macro keeps its text (#455), and
    // 1.4 folds a const where the declaration is written (#1664 box 7)
    if (sizeExpr) {
      return DimensionResolver.resolve(sizeExpr);
    }
 
    // Issue #636: Empty dimension [] - infer size from array initializer
    return { size: VariableCollector.omittedSize(initExpr), expr: null };
  }
 
  /**
   * #1822: an omitted size, counted from a one-dimensional declaration's list
   * or string literal. With nothing to count it is E0892, and it is recorded
   * as "" -- unknown, as a C macro dimension is -- so the array keeps its
   * rank: dropping it made `u8[2][] rows` one-dimensional, and an includer's
   * `rows[1][2][0]` a false E0856 (#1824 review).
   */
  private static omittedSize(
    countedFrom: Parser.ExpressionContext | null,
  ): number | string {
    return (
      (countedFrom && ArrayInitializerUtils.getInferredSize(countedFrom)) ?? ""
    );
  }
 
  /**
   * Collect array dimensions from a variable declaration.
   */
  private static collectArrayDimensions(
    arrayDims: Parser.ArrayDimensionContext[],
    initExpr: Parser.ExpressionContext | null,
  ): IDeclaredDimension[] {
    const dimensions: IDeclaredDimension[] = [];
 
    for (const dim of arrayDims) {
      dimensions.push(VariableCollector.resolveDimension(dim, initExpr));
    }
 
    return dimensions;
  }
 
  /**
   * Collect dimensions from C-Next style arrayType syntax (u16[8] arr, u16[4][4] arr, u16[] arr).
   * Handles size inference from initializer when dimension is empty.
   */
  private static collectArrayTypeDimensions(
    arrayTypeCtx: Parser.ArrayTypeContext,
    initExpr: Parser.ExpressionContext | null,
  ): IDeclaredDimension[] {
    const dimensions: IDeclaredDimension[] = [];
    for (const dim of arrayTypeCtx.arrayTypeDimension()) {
      const sizeExpr = dim.expression();
 
      if (!sizeExpr) {
        // Issue #636: Empty dimension [] - infer size from array initializer
        dimensions.push({
          size: VariableCollector.omittedSize(initExpr),
          expr: null,
        });
        continue;
      }
 
      // Issue #1127: fold through the shared resolver so a dimension resolves
      // identically here and in codegen. Folding only literals and consts here
      // meant `u8[sizeof(u32)]` reached the header as `sizeof(u32)` -- a
      // C-Next type name in generated C, which does not compile -- while the
      // .c correctly said [4]. Text that does not fold is still kept, for macro and
      // enum references.
      dimensions.push(DimensionResolver.resolve(sizeExpr));
    }
    return dimensions;
  }
 
  /**
   * What a declaration SAYS: its type, modifiers, dimensions and initializer.
   *
   * #1668: shared by a global or scope member (`collect`, below) and a local
   * or `for` variable (`LexicalScopeCollector`), so a declaration means the
   * same thing wherever it is written. No const folds here: a literal or
   * `sizeof` dimension folds, and every name stays its text for 1.4 Resolve
   * to fold in the declaration's lexical environment (#1664 box 7).
   */
  static declaredFacts(
    ctx: Parser.VariableDeclarationContext | Parser.ForVarDeclContext,
    scopePath: string,
    isScopeType?: (qualifiedName: string) => boolean,
  ): {
    type: TType;
    isConst: boolean;
    isAtomic: boolean;
    isVolatile: boolean;
    overflowBehavior: TOverflowBehavior;
    isArray: boolean;
    arrayDimensions: (number | string)[];
    arrayDimensionExprs: (TConstExpr | null)[];
    initialValue: string | undefined;
    initialValueExpr: TConstExpr | null;
    initializerCallee: string | null;
  } {
    // Get type string and convert to TType
    const typeCtx = ctx.type()!;
    // #1298: members carry the scope's PATH, not the scope object. The path
    // holds every outer component, so nothing downstream can flatten it to a
    // leaf -- which is what the reference threaded here used to protect against.
    const typeStr = TypeUtils.getTypeName(typeCtx, scopePath, isScopeType);
    const type = VariableCollector.resolveDeclaredType(
      typeStr,
      ctx,
      TypeUtils.resolveType(typeCtx, scopePath, isScopeType),
    );
 
    // Check for const modifier (a `for` variable has none)
    const isConst =
      ctx instanceof Parser.VariableDeclarationContext &&
      ctx.constModifier() !== null;
 
    // Issue #468: Check for atomic modifier
    const isAtomic = ctx.atomicModifier() !== null;
    const isVolatile = ctx.volatileModifier() !== null;
 
    // Issue #1303: ADR-044's clamp/wrap is a declared fact like const and
    // volatile above, so it is read HERE and carried on the symbol. Reading it
    // only in codegen meant it existed for the declaring file and nowhere else.
    const overflowBehavior = OverflowBehaviorUtils.fromModifier(
      ctx.overflowModifier(),
    );
 
    // Check for array dimensions - both C-style (arrayDimension) and C-Next style (arrayType)
    const arrayDims = ctx.arrayDimension();
    const arrayTypeCtx = typeCtx.arrayType();
    const hasArrayTypeSyntax = arrayTypeCtx !== null;
    const isArray = arrayDims.length > 0 || hasArrayTypeSyntax;
    const initExpr = ctx.expression();
    const declaredDimensions: IDeclaredDimension[] = [];
 
    // #1822 (ADR-035): only a one-dimensional array's size is counted from its
    // list. An empty dimension anywhere else is E0892, and counting the OUTER
    // list for it invented a size that another file then bounds-checked
    // against (`u8[2][] m` read as [2][2] for rows of three).
    const dimensionCount =
      (arrayTypeCtx?.arrayTypeDimension().length ?? 0) + arrayDims.length;
    const countedFrom = dimensionCount === 1 ? initExpr : null;
 
    // Collect dimensions from arrayType syntax (u16[8] arr, u16[4][4] arr, u16[] arr)
    if (hasArrayTypeSyntax) {
      declaredDimensions.push(
        ...VariableCollector.collectArrayTypeDimensions(
          arrayTypeCtx,
          countedFrom,
        ),
      );
    }
 
    // Collect additional dimensions from arrayDimension syntax
    if (arrayDims.length > 0) {
      declaredDimensions.push(
        ...VariableCollector.collectArrayDimensions(arrayDims, countedFrom),
      );
    }
 
    // Issue #282: Capture initial value for const inlining
    const initialValue = initExpr?.getText();
    const arrayDimensions = declaredDimensions.map((dim) => dim.size);
    const arrayDimensionExprs = declaredDimensions.map((dim) => dim.expr);
 
    return {
      type,
      isConst,
      isAtomic,
      isVolatile,
      overflowBehavior,
      isArray,
      arrayDimensions,
      arrayDimensionExprs,
      initialValue,
      // #1175: a const's value folds from this, in 1.4, never from the text
      initialValueExpr:
        isConst && initExpr ? ConstExprLowering.lower(initExpr) : null,
      // #895: what the initializer calls, for `DeclaredPointer.of`
      initializerCallee: VariableCollector.calleeOf(initExpr),
    };
  }
 
  /**
   * The function an initializer calls, when it is shaped `f(...)` or
   * `global.f(...)` -- source text, recorded on the declaration. Whether it
   * names a C function is `DeclaredPointer.of`'s question, asked of the
   * headers once they are known. The call is the chain's LAST operation
   * (#1760 review): `f()[2]` is an element of what `f` returns, not it.
   */
  private static calleeOf(
    expr: Parser.ExpressionContext | null,
  ): string | null {
    const postfix = expr
      ? ExpressionUnwrapper.getPostfixExpression(expr)
      : null;
    if (!postfix) return null;
    const primary = postfix.primaryExpression();
    const ops = postfix.postfixOp();
    if (primary.GLOBAL()) {
      const member = ops[0]?.IDENTIFIER();
      return member && ops.length === 2 && VariableCollector.isCall(ops[1])
        ? member.getText()
        : null;
    }
    const identifier = primary.IDENTIFIER();
    return identifier && ops.length === 1 && VariableCollector.isCall(ops[0])
      ? identifier.getText()
      : null;
  }
 
  private static isCall(op: Parser.PostfixOpContext | undefined): boolean {
    return (
      op !== undefined &&
      Boolean(op.argumentList() || op.getText().startsWith("("))
    );
  }
 
  /**
   * A global or scope member: its identity, and what its declaration says.
   * @param ctx The variable declaration context
   * @param sourceFile Source file path
   * @param scopePath The path of the scope this variable belongs to (dotted path, "" at file scope)
   * @param visibility Required: #1161 -- a default here is a third source of
   *   truth for one fact, which is how #1300 happened to the type kinds
   * @param isScopeType ADR-057 predicate: is this *qualified* name a scope type?
   * @returns The variable symbol with TType-based types and scope reference
   */
  static collect(
    ctx: Parser.VariableDeclarationContext,
    sourceFile: string,
    scopePath: string,
    visibility: TVisibility,
    isScopeType?: (qualifiedName: string) => boolean,
  ): IVariableSymbol {
    const name = ctx.IDENTIFIER().getText();
    const span = ParserUtils.getSpan(ctx);
    const facts = VariableCollector.declaredFacts(ctx, scopePath, isScopeType);
 
    // Build base symbol
    const symbol: IVariableSymbol = {
      kind: "variable",
      name,
      scopePath,
      // #1285: identity computed once, from the scope chain, not
      // re-derived by every consumer.
      ...ScopeUtils.identityOf({ name, scopePath }),
      sourceFile,
      span,
      sourceLanguage: ESourceLanguage.CNext,
      visibility,
      type: facts.type,
      isConst: facts.isConst,
      isAtomic: facts.isAtomic,
      isVolatile: facts.isVolatile,
      overflowBehavior: facts.overflowBehavior,
      isArray: facts.isArray,
      arrayDimensions:
        facts.arrayDimensions.length > 0 ? facts.arrayDimensions : undefined,
      arrayDimensionExprs:
        facts.arrayDimensions.length > 0
          ? facts.arrayDimensionExprs
          : undefined,
      initialValue: facts.initialValue,
      initialValueExpr: facts.initialValueExpr ?? undefined,
      initializerCallee: facts.initializerCallee,
    };
 
    return symbol;
  }
}
 
export default VariableCollector;