All files / transpiler/output/codegen/assignment AssignmentClassifier.ts

92.88% Statements 235/253
87.45% Branches 223/255
100% Functions 28/28
93.85% Lines 229/244

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 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829                                                                    182x                         245x 187x   58x               357x 357x 6x         351x 351x 11x       340x 340x 12x       328x 328x 45x       283x 283x 32x       251x 251x 7x         244x 11x       233x                     357x 291x     66x 66x       66x 53x     13x 9x             4x 3x     1x                   53x 53x 50x     3x       3x       3x                           9x 1x       1x 1x       1x 1x             8x 8x       2x     6x       6x       6x       6x 1x     5x                 3x 3x 2x     1x 1x       1x       1x       1x       1x 1x                           351x 329x       22x       22x 22x 22x       22x         22x 2x             20x 20x       20x 7x     13x                                 2x       2x     2x       1x     1x                   20x     20x 1x 1x 1x             19x 6x         13x                     13x       13x 11x     2x       2x 2x                       340x 314x     26x 4x     22x 8x     14x             4x   4x 1x     1x     3x             8x       8x 8x   8x 6x             2x       1x       1x 1x       1x                       6x 2x 2x           4x     4x           6x   1x   1x   2x                             328x 83x     245x 200x     45x 45x         328x           328x       37x     8x 8x         1x   7x     29x         4x   25x     1x     2x     5x                     283x 198x         85x 81x 81x 4x   1x 1x 1x 3x   1x 1x   2x     83x 2x           81x 283x 3x           78x 78x         29x     49x                 251x 170x 170x 170x                     245x 10x 10x 10x 10x                     245x 2x 2x 2x                   58x 58x       13x   45x                     58x     58x 13x     45x       58x                   245x 197x     48x 48x 12x   36x                       244x           234x     10x 10x 1x     9x 9x 9x   9x         244x           244x                       251x 251x     245x 245x     245x 245x     245x 245x       245x     244x       244x   244x                     13x 13x 8x   5x                     3x 3x          
/**
 * Assignment classifier for dispatch-based code generation (ADR-109).
 *
 * Analyzes an assignment context and determines which AssignmentKind it is.
 * The classification order matches the original generateAssignment() method's
 * if-else chain to ensure identical behavior.
 *
 * Migrated to use CodeGenState instead of constructor DI.
 */
import AssignmentKind from "./AssignmentKind";
import IAssignmentContext from "./IAssignmentContext";
import CodeGenState from "../../../state/CodeGenState";
import SubscriptClassifier from "../subscript/SubscriptClassifier";
import TTypeInfo from "../types/TTypeInfo";
import TypeCheckUtils from "../../../../utils/TypeCheckUtils";
import QualifiedNameGenerator from "../utils/QualifiedNameGenerator";
 
/**
 * Classifies assignment statements by analyzing their structure.
 *
 * Classification priority (higher = checked first):
 * 1. Bitmap field assignments (memberAccess patterns)
 * 2. Register bit/bitmap assignments
 * 3. Global/this prefix patterns
 * 4. Array/bit access patterns
 * 5. Atomic/overflow special cases
 * 6. String assignments
 * 7. Simple fallback
 */
class AssignmentClassifier {
  /**
   * Check if typeInfo represents a simple string type (not a 2D+ string array).
   */
  private static isSimpleStringType(typeInfo: TTypeInfo | undefined): boolean {
    return (
      typeInfo?.isString === true &&
      typeInfo.stringCapacity !== undefined &&
      (!typeInfo.arrayDimensions || typeInfo.arrayDimensions.length <= 1)
    );
  }
 
  /**
   * Extract struct name and field name from a 2-identifier context.
   */
  private static getStructFieldNames(
    ctx: IAssignmentContext,
  ): { structName: string; fieldName: string } | null {
    if (ctx.identifiers.length !== 2) {
      return null;
    }
    return { structName: ctx.identifiers[0], fieldName: ctx.identifiers[1] };
  }
 
  /**
   * Classify an assignment context into an AssignmentKind.
   */
  static classify(ctx: IAssignmentContext): AssignmentKind {
    // === Priority 1: Bitmap field assignments ===
    const bitmapKind = AssignmentClassifier.classifyBitmapField(ctx);
    if (bitmapKind !== null) {
      return bitmapKind;
    }
 
    // === Priority 2: Member access with subscripts (arrays, register bits) ===
    const memberSubscriptKind =
      AssignmentClassifier.classifyMemberWithSubscript(ctx);
    if (memberSubscriptKind !== null) {
      return memberSubscriptKind;
    }
 
    // === Priority 3: Global/this prefix patterns ===
    const prefixKind = AssignmentClassifier.classifyPrefixPattern(ctx);
    if (prefixKind !== null) {
      return prefixKind;
    }
 
    // === Priority 4: Simple array/bit access ===
    const arrayBitKind = AssignmentClassifier.classifyArrayOrBitAccess(ctx);
    if (arrayBitKind !== null) {
      return arrayBitKind;
    }
 
    // === Priority 5: Atomic/overflow compound assignments ===
    const specialKind = AssignmentClassifier.classifySpecialCompound(ctx);
    if (specialKind !== null) {
      return specialKind;
    }
 
    // === Priority 6: String assignments ===
    const stringKind = AssignmentClassifier.classifyStringAssignment(ctx);
    if (stringKind !== null) {
      return stringKind;
    }
 
    // === Priority 7: Member chain fallback ===
    // Any member access with subscripts that didn't match a more specific pattern
    if (ctx.hasMemberAccess && ctx.hasArrayAccess) {
      return AssignmentKind.MEMBER_CHAIN;
    }
 
    // === Fallback: Simple assignment ===
    return AssignmentKind.SIMPLE;
  }
 
  /**
   * Classify bitmap field assignments.
   * Patterns: var.field, struct.bitmapMember.field, REG.MEMBER.field, Scope.REG.MEMBER.field
   */
  private static classifyBitmapField(
    ctx: IAssignmentContext,
  ): AssignmentKind | null {
    // Must have member access without subscripts
    if (!ctx.hasMemberAccess || ctx.hasArrayAccess) {
      return null;
    }
 
    const ids = ctx.identifiers;
    Iif (ctx.subscripts.length !== 0) {
      return null;
    }
 
    if (ids.length === 2) {
      return AssignmentClassifier.classifySimpleBitmapField(ids[0], ids[1]);
    }
 
    if (ids.length === 3) {
      return AssignmentClassifier.classifyThreeIdBitmapField(
        ids[0],
        ids[1],
        ids[2],
      );
    }
 
    if (ids.length === 4) {
      return AssignmentClassifier.classifyScopedRegisterBitmapField(ids);
    }
 
    return null;
  }
 
  /**
   * Classify 2-id bitmap field: var.field
   */
  private static classifySimpleBitmapField(
    varName: string,
    fieldName: string,
  ): AssignmentKind | null {
    const typeInfo = CodeGenState.getVariableTypeInfo(varName);
    if (!typeInfo?.isBitmap || !typeInfo.bitmapTypeName) {
      return null;
    }
 
    const width = AssignmentClassifier.lookupBitmapFieldWidth(
      typeInfo.bitmapTypeName,
      fieldName,
    );
    Iif (width === null) {
      return null;
    }
 
    return width === 1
      ? AssignmentKind.BITMAP_FIELD_SINGLE_BIT
      : AssignmentKind.BITMAP_FIELD_MULTI_BIT;
  }
 
  /**
   * Classify 3-id bitmap field: REG.MEMBER.field or struct.bitmapMember.field
   */
  private static classifyThreeIdBitmapField(
    firstName: string,
    secondName: string,
    fieldName: string,
  ): AssignmentKind | null {
    // Check if register member bitmap field: REG.MEMBER.field
    if (CodeGenState.symbols!.knownRegisters.has(firstName)) {
      const bitmapType = AssignmentClassifier.lookupRegisterMemberBitmapType(
        firstName,
        secondName,
      );
      Eif (bitmapType) {
        const width = AssignmentClassifier.lookupBitmapFieldWidth(
          bitmapType,
          fieldName,
        );
        Eif (width !== null) {
          return AssignmentKind.REGISTER_MEMBER_BITMAP_FIELD;
        }
      }
      return null;
    }
 
    // Check if struct member bitmap field: struct.bitmapMember.field
    const structTypeInfo = CodeGenState.getVariableTypeInfo(firstName);
    if (
      !structTypeInfo ||
      !CodeGenState.isKnownStruct(structTypeInfo.baseType)
    ) {
      return null;
    }
 
    const memberInfo = CodeGenState.getMemberTypeInfo(
      structTypeInfo.baseType,
      secondName,
    );
    Iif (!memberInfo) {
      return null;
    }
 
    const width = AssignmentClassifier.lookupBitmapFieldWidth(
      memberInfo.baseType,
      fieldName,
    );
    if (width !== null) {
      return AssignmentKind.STRUCT_MEMBER_BITMAP_FIELD;
    }
 
    return null;
  }
 
  /**
   * Classify 4-id scoped register bitmap field: Scope.REG.MEMBER.field
   */
  private static classifyScopedRegisterBitmapField(
    ids: readonly string[],
  ): AssignmentKind | null {
    const scopeName = ids[0];
    if (!CodeGenState.isKnownScope(scopeName)) {
      return null;
    }
 
    const fullRegName = QualifiedNameGenerator.forMember(scopeName, ids[1]);
    Iif (!CodeGenState.symbols!.knownRegisters.has(fullRegName)) {
      return null;
    }
 
    const bitmapType = AssignmentClassifier.lookupRegisterMemberBitmapType(
      fullRegName,
      ids[2],
    );
    Iif (!bitmapType) {
      return null;
    }
 
    const width = AssignmentClassifier.lookupBitmapFieldWidth(
      bitmapType,
      ids[3],
    );
    Eif (width !== null) {
      return AssignmentKind.SCOPED_REGISTER_MEMBER_BITMAP_FIELD;
    }
 
    return null;
  }
 
  /**
   * Classify member access with subscripts.
   * Patterns: arr[i][j], struct.arr[i], REG.MEMBER[bit], matrix[i][j][bit]
   */
  private static classifyMemberWithSubscript(
    ctx: IAssignmentContext,
  ): AssignmentKind | null {
    // Need subscripts through memberAccess pattern
    if (!ctx.hasMemberAccess || ctx.subscripts.length === 0) {
      return null;
    }
 
    // Skip this.* and global.* patterns - they're handled by classifyPrefixPattern
    Iif (ctx.hasThis || ctx.hasGlobal) {
      return null;
    }
 
    const ids = ctx.identifiers;
    const firstId = ids[0];
    const typeInfo = CodeGenState.getVariableTypeInfo(firstId);
 
    // Check for bit range through struct chain: devices[0].control[0, 4]
    // Detected by last subscript having 2 expressions (start, width)
    Iif (ctx.lastSubscriptExprCount === 2) {
      return AssignmentKind.STRUCT_CHAIN_BIT_RANGE;
    }
 
    // Multi-dimensional array element: arr[i][j] (1 identifier, multiple subscripts)
    if (ids.length === 1) {
      return AssignmentClassifier.classifyMultiDimArrayAccess(
        typeInfo,
        ctx.subscripts.length,
      );
    }
 
    // 2+ identifiers with subscripts: register bit or bitmap array
    Eif (ids.length >= 2) {
      const registerKind = AssignmentClassifier.classifyRegisterBitAccess(
        ids,
        ctx.subscripts.length,
      );
      if (registerKind !== null) {
        return registerKind;
      }
 
      return AssignmentClassifier.classifyBitmapArrayField(
        ids[1],
        typeInfo,
        ctx.subscripts.length,
      );
    }
 
    return null;
  }
 
  /**
   * Classify multi-dimensional array access: arr[i][j] or arr[i][j][bit]
   */
  private static classifyMultiDimArrayAccess(
    typeInfo: TTypeInfo | undefined,
    subscriptCount: number,
  ): AssignmentKind | null {
    Iif (!typeInfo?.isArray || !typeInfo.arrayDimensions) {
      return null;
    }
 
    const numDims = typeInfo.arrayDimensions.length;
 
    // Check for bit indexing on array element
    if (
      subscriptCount === numDims + 1 &&
      TypeCheckUtils.isInteger(typeInfo.baseType)
    ) {
      return AssignmentKind.ARRAY_ELEMENT_BIT;
    }
 
    return AssignmentKind.MULTI_DIM_ARRAY_ELEMENT;
  }
 
  /**
   * Classify register bit access: REG.MEMBER[bit] or Scope.REG.MEMBER[bit]
   */
  private static classifyRegisterBitAccess(
    ids: readonly string[],
    subscriptCount: number,
  ): AssignmentKind | null {
    const firstId = ids[0];
 
    // Check for scoped register: Scope.REG.MEMBER[bit]
    if (CodeGenState.isKnownScope(firstId) && ids.length >= 3) {
      const scopedRegName = `${firstId}_${ids[1]}`;
      Eif (CodeGenState.symbols!.knownRegisters.has(scopedRegName)) {
        return subscriptCount === 2
          ? AssignmentKind.REGISTER_BIT_RANGE
          : AssignmentKind.REGISTER_BIT;
      }
    }
 
    // Check for non-scoped register: REG.MEMBER[bit]
    if (CodeGenState.symbols!.knownRegisters.has(firstId)) {
      return subscriptCount === 2
        ? AssignmentKind.REGISTER_BIT_RANGE
        : AssignmentKind.REGISTER_BIT;
    }
 
    return null;
  }
 
  /**
   * Classify bitmap array element field: bitmapArr[i].field
   */
  private static classifyBitmapArrayField(
    secondId: string,
    typeInfo: TTypeInfo | undefined,
    subscriptCount: number,
  ): AssignmentKind | null {
    Iif (subscriptCount !== 1) {
      return null;
    }
 
    if (!typeInfo?.isBitmap || !typeInfo.isArray || !typeInfo.bitmapTypeName) {
      return null;
    }
 
    const width = AssignmentClassifier.lookupBitmapFieldWidth(
      typeInfo.bitmapTypeName,
      secondId,
    );
    Eif (width !== null) {
      return AssignmentKind.BITMAP_ARRAY_ELEMENT_FIELD;
    }
 
    return null;
  }
 
  /**
   * Classify global.* and this.* prefix patterns.
   */
  private static classifyPrefixPattern(
    ctx: IAssignmentContext,
  ): AssignmentKind | null {
    if (!ctx.hasGlobal && !ctx.hasThis) {
      return null;
    }
 
    if (ctx.hasGlobal && ctx.postfixOpsCount > 0) {
      return AssignmentClassifier.classifyGlobalPrefix(ctx);
    }
 
    if (ctx.hasThis && ctx.postfixOpsCount > 0) {
      return AssignmentClassifier.classifyThisPrefix(ctx);
    }
 
    return null;
  }
 
  /**
   * Classify global.* patterns: global.reg[bit], global.arr[i], global.member
   */
  private static classifyGlobalPrefix(ctx: IAssignmentContext): AssignmentKind {
    const firstId = ctx.identifiers[0];
 
    if (ctx.hasArrayAccess) {
      Iif (CodeGenState.symbols!.knownRegisters.has(firstId)) {
        return AssignmentKind.GLOBAL_REGISTER_BIT;
      }
      return AssignmentKind.GLOBAL_ARRAY;
    }
 
    return AssignmentKind.GLOBAL_MEMBER;
  }
 
  /**
   * Classify this.* patterns: this.reg[bit], this.member, this.REG.MEMBER.field
   */
  private static classifyThisPrefix(ctx: IAssignmentContext): AssignmentKind {
    Iif (!CodeGenState.currentScope) {
      return AssignmentKind.THIS_MEMBER;
    }
 
    const firstId = ctx.identifiers[0];
    const scopedRegName = `${CodeGenState.currentScope}_${firstId}`;
 
    if (ctx.hasArrayAccess) {
      return AssignmentClassifier.classifyThisWithArrayAccess(
        ctx,
        scopedRegName,
      );
    }
 
    // this.REG.MEMBER.field (scoped register bitmap field)
    if (
      ctx.identifiers.length === 3 &&
      CodeGenState.symbols!.knownRegisters.has(scopedRegName)
    ) {
      const bitmapType = AssignmentClassifier.lookupRegisterMemberBitmapType(
        scopedRegName,
        ctx.identifiers[1],
      );
      Eif (bitmapType) {
        return AssignmentKind.SCOPED_REGISTER_MEMBER_BITMAP_FIELD;
      }
    }
 
    return AssignmentKind.THIS_MEMBER;
  }
 
  /**
   * Classify this.reg[bit] / this.arr[i] / this.flags[3] patterns with array access.
   * Issue #954: Uses SubscriptClassifier to distinguish array vs bit access.
   */
  private static classifyThisWithArrayAccess(
    ctx: IAssignmentContext,
    scopedRegName: string,
  ): AssignmentKind {
    // Check for scoped register first
    if (CodeGenState.symbols!.knownRegisters.has(scopedRegName)) {
      const hasBitRange = ctx.postfixOps.some((op) => op.COMMA() !== null);
      return hasBitRange
        ? AssignmentKind.SCOPED_REGISTER_BIT_RANGE
        : AssignmentKind.SCOPED_REGISTER_BIT;
    }
 
    // Get type info using resolved scoped name (e.g., "Sensor_value")
    const typeInfo = CodeGenState.getVariableTypeInfo(scopedRegName);
 
    // Use shared classifier to determine array vs bit access
    const subscriptKind = SubscriptClassifier.classify({
      typeInfo: typeInfo ?? null,
      subscriptCount: ctx.lastSubscriptExprCount,
      isRegisterAccess: false,
    });
 
    switch (subscriptKind) {
      case "bit_single":
        return AssignmentKind.THIS_BIT;
      case "bit_range":
        return AssignmentKind.THIS_BIT_RANGE;
      default:
        return AssignmentKind.THIS_ARRAY;
    }
  }
 
  /**
   * Classify simple array/bit access (no prefix, no member access).
   * Pattern: arr[i] or flags[bit]
   *
   * Issue #579: Uses shared SubscriptClassifier to ensure consistent behavior
   * with the expression path in CodeGenerator._generatePostfixExpr.
   */
  private static classifyArrayOrBitAccess(
    ctx: IAssignmentContext,
  ): AssignmentKind | null {
    // Must have arrayAccess without memberAccess or prefix
    if (ctx.hasGlobal || ctx.hasThis || ctx.hasMemberAccess) {
      return null;
    }
 
    if (!ctx.hasArrayAccess || ctx.subscripts.length === 0) {
      return null;
    }
 
    const name = ctx.identifiers[0];
    const typeInfo = CodeGenState.getVariableTypeInfo(name) ?? null;
 
    // Use shared classifier for array vs bit access decision
    // Use lastSubscriptExprCount to distinguish [0][0] (two ops, each 1 expr)
    // from [0, 5] (one op, 2 exprs)
    const subscriptKind = SubscriptClassifier.classify({
      typeInfo,
      subscriptCount: ctx.lastSubscriptExprCount,
      isRegisterAccess: false,
    });
 
    switch (subscriptKind) {
      case "array_element":
        // Multi-dimensional array: matrix[i][j] has multiple subscript operations
        // but each with 1 expression (vs slice [0, 5] with 2 expressions in 1 op)
        if (ctx.subscripts.length > 1) {
          // Check if last subscript is bit access on an integer array element
          // e.g., matrix[i][j][bit] where matrix is 2D integer array
          const numDims = typeInfo?.arrayDimensions?.length ?? 0;
          if (
            ctx.subscripts.length === numDims + 1 &&
            typeInfo &&
            TypeCheckUtils.isInteger(typeInfo.baseType)
          ) {
            return AssignmentKind.ARRAY_ELEMENT_BIT;
          }
          return AssignmentKind.MULTI_DIM_ARRAY_ELEMENT;
        }
        // String array element (special case for 2D string arrays)
        if (
          typeInfo?.isString &&
          typeInfo.arrayDimensions &&
          typeInfo.arrayDimensions.length > 1
        ) {
          return AssignmentKind.STRING_ARRAY_ELEMENT;
        }
        return AssignmentKind.ARRAY_ELEMENT;
 
      case "array_slice":
        return AssignmentKind.ARRAY_SLICE;
 
      case "bit_single":
        return AssignmentKind.INTEGER_BIT;
 
      case "bit_range":
        return AssignmentKind.INTEGER_BIT_RANGE;
    }
  }
 
  /**
   * Classify atomic and overflow-clamped compound assignments.
   * Handles simple identifiers, this.member, and global.member patterns.
   */
  private static classifySpecialCompound(
    ctx: IAssignmentContext,
  ): AssignmentKind | null {
    if (!ctx.isCompound) {
      return null;
    }
 
    // Get typeInfo based on target pattern
    let typeInfo;
    if (ctx.isSimpleIdentifier) {
      const id = ctx.identifiers[0];
      typeInfo = CodeGenState.getVariableTypeInfo(id);
    } else if (ctx.isSimpleThisAccess && CodeGenState.currentScope) {
      // this.member pattern: lookup using scoped name
      const memberName = ctx.identifiers[0];
      const scopedName = `${CodeGenState.currentScope}_${memberName}`;
      typeInfo = CodeGenState.getVariableTypeInfo(scopedName);
    } else if (ctx.isSimpleGlobalAccess) {
      // global.member pattern: lookup using direct name
      const memberName = ctx.identifiers[0];
      typeInfo = CodeGenState.getVariableTypeInfo(memberName);
    } else {
      return null;
    }
 
    if (!typeInfo) {
      return null;
    }
 
    // Atomic RMW - for global atomic variables (simple identifiers or global.member)
    // Scoped atomics (this.member) use overflow behavior, not LDREX/STREX
    const isGlobalAtomic =
      typeInfo.isAtomic && (ctx.isSimpleIdentifier || ctx.isSimpleGlobalAccess);
    if (isGlobalAtomic) {
      return AssignmentKind.ATOMIC_RMW;
    }
 
    // Overflow clamp (integers only, not floats)
    // Only applies to arithmetic compound ops (+= -= *=) which can overflow
    // Bitwise ops (&= |= ^= <<= >>=) don't overflow, so they go to SIMPLE
    const ARITHMETIC_COMPOUND_OPS = new Set(["+=", "-=", "*="]);
    if (
      typeInfo.overflowBehavior === "clamp" &&
      TypeCheckUtils.isInteger(typeInfo.baseType) &&
      ARITHMETIC_COMPOUND_OPS.has(ctx.cOp)
    ) {
      return AssignmentKind.OVERFLOW_CLAMP;
    }
 
    return null;
  }
 
  /**
   * Check if a simple identifier is a string variable.
   */
  private static _classifySimpleStringVar(
    ctx: IAssignmentContext,
  ): AssignmentKind | null {
    if (!ctx.isSimpleIdentifier) return null;
    const id = ctx.identifiers[0];
    const typeInfo = CodeGenState.getVariableTypeInfo(id);
    return AssignmentClassifier.isSimpleStringType(typeInfo)
      ? AssignmentKind.STRING_SIMPLE
      : null;
  }
 
  /**
   * Check if this.member is a string.
   */
  private static _classifyThisMemberString(
    ctx: IAssignmentContext,
  ): AssignmentKind | null {
    if (!ctx.isSimpleThisAccess || !CodeGenState.currentScope) return null;
    const memberName = ctx.identifiers[0];
    const scopedName = `${CodeGenState.currentScope}_${memberName}`;
    const typeInfo = CodeGenState.getVariableTypeInfo(scopedName);
    return AssignmentClassifier.isSimpleStringType(typeInfo)
      ? AssignmentKind.STRING_THIS_MEMBER
      : null;
  }
 
  /**
   * Check if global.member is a string.
   */
  private static _classifyGlobalString(
    ctx: IAssignmentContext,
  ): AssignmentKind | null {
    if (!ctx.isSimpleGlobalAccess) return null;
    const id = ctx.identifiers[0];
    const typeInfo = CodeGenState.getVariableTypeInfo(id);
    return AssignmentClassifier.isSimpleStringType(typeInfo)
      ? AssignmentKind.STRING_GLOBAL
      : null;
  }
 
  /**
   * Resolve struct type from variable name.
   * Returns the base struct type if valid, null if not a known struct.
   */
  private static _resolveStructType(structName: string): string | null {
    const structTypeInfo = CodeGenState.getVariableTypeInfo(structName);
    if (
      !structTypeInfo ||
      !CodeGenState.isKnownStruct(structTypeInfo.baseType)
    ) {
      return null;
    }
    return structTypeInfo.baseType;
  }
 
  /**
   * Resolve struct field type from struct variable name and field name.
   * Returns null if struct type can't be resolved or field doesn't exist.
   */
  private static _resolveStructFieldType(structFieldNames: {
    structName: string;
    fieldName: string;
  }): { structType: string; fieldType: string | undefined } | null {
    const structType = AssignmentClassifier._resolveStructType(
      structFieldNames.structName,
    );
    if (!structType) {
      return null;
    }
    // Issue #831: Use SymbolTable as single source of truth for struct fields
    const fieldType = CodeGenState.symbolTable?.getStructFieldType(
      structType,
      structFieldNames.fieldName,
    );
    return { structType, fieldType };
  }
 
  /**
   * Check if struct.field is a string field.
   */
  private static _classifyStructFieldString(
    ctx: IAssignmentContext,
    structFieldNames: { structName: string; fieldName: string } | null,
  ): AssignmentKind | null {
    if (!ctx.hasMemberAccess || ctx.hasArrayAccess || !structFieldNames) {
      return null;
    }
    const resolved =
      AssignmentClassifier._resolveStructFieldType(structFieldNames);
    if (!resolved) {
      return null;
    }
    return resolved.fieldType && TypeCheckUtils.isString(resolved.fieldType)
      ? AssignmentKind.STRING_STRUCT_FIELD
      : null;
  }
 
  /**
   * Check if struct.arr[i] is a string array element.
   */
  private static _classifyStructArrayElementString(
    ctx: IAssignmentContext,
    structFieldNames: { structName: string; fieldName: string } | null,
  ): AssignmentKind | null {
    if (
      !ctx.hasMemberAccess ||
      !ctx.hasArrayAccess ||
      !structFieldNames ||
      ctx.subscripts.length !== 1
    ) {
      return null;
    }
    const resolved =
      AssignmentClassifier._resolveStructFieldType(structFieldNames);
    if (!resolved) {
      return null;
    }
 
    const { structType, fieldType } = resolved;
    const { fieldName } = structFieldNames;
    const fieldArrays = CodeGenState.symbols!.structFieldArrays.get(structType);
    const dimensions =
      CodeGenState.symbols!.structFieldDimensions.get(structType)?.get(
        fieldName,
      );
 
    const isStringArrayField =
      fieldType &&
      TypeCheckUtils.isString(fieldType) &&
      fieldArrays?.has(fieldName) &&
      dimensions &&
      dimensions.length >= 1;
 
    return isStringArrayField
      ? AssignmentKind.STRING_STRUCT_ARRAY_ELEMENT
      : null;
  }
 
  /**
   * Classify string assignments.
   */
  private static classifyStringAssignment(
    ctx: IAssignmentContext,
  ): AssignmentKind | null {
    // Simple string variable
    const simpleVar = AssignmentClassifier._classifySimpleStringVar(ctx);
    if (simpleVar) return simpleVar;
 
    // this.member string
    const thisMember = AssignmentClassifier._classifyThisMemberString(ctx);
    Iif (thisMember) return thisMember;
 
    // global.member string
    const globalMember = AssignmentClassifier._classifyGlobalString(ctx);
    Iif (globalMember) return globalMember;
 
    // struct.field or struct.arr[i] string
    const structFieldNames = AssignmentClassifier.getStructFieldNames(ctx);
    const structField = AssignmentClassifier._classifyStructFieldString(
      ctx,
      structFieldNames,
    );
    if (structField) return structField;
 
    const structArrayElement =
      AssignmentClassifier._classifyStructArrayElementString(
        ctx,
        structFieldNames,
      );
    Iif (structArrayElement) return structArrayElement;
 
    return null;
  }
 
  /**
   * Look up a bitmap field's width by bitmap type name and field name.
   * Returns the field width if found, or null if the bitmap/field doesn't exist.
   */
  private static lookupBitmapFieldWidth(
    bitmapTypeName: string,
    fieldName: string,
  ): number | null {
    const fields = CodeGenState.symbols!.bitmapFields.get(bitmapTypeName);
    if (fields?.has(fieldName)) {
      return fields.get(fieldName)!.width;
    }
    return null;
  }
 
  /**
   * Look up the bitmap type for a register member (e.g., "REG_MEMBER" -> "BitmapType").
   * Returns the bitmap type name if found, or null.
   */
  private static lookupRegisterMemberBitmapType(
    registerName: string,
    memberName: string,
  ): string | null {
    const key = `${registerName}_${memberName}`;
    return CodeGenState.symbols!.registerMemberTypes.get(key) ?? null;
  }
}
 
export default AssignmentClassifier;