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 | 328x 175x 235x 180x 55x 328x 328x 6x 322x 322x 10x 312x 312x 9x 303x 303x 32x 271x 271x 30x 241x 241x 7x 234x 10x 224x 328x 265x 63x 63x 63x 51x 12x 8x 4x 3x 1x 51x 51x 48x 3x 3x 3x 8x 1x 1x 1x 1x 1x 7x 7x 2x 5x 5x 5x 5x 1x 4x 3x 3x 2x 1x 1x 1x 1x 1x 1x 1x 322x 302x 20x 20x 20x 20x 20x 20x 2x 18x 18x 18x 7x 11x 2x 2x 2x 1x 1x 18x 18x 1x 1x 1x 17x 6x 11x 11x 11x 10x 1x 1x 1x 312x 288x 24x 4x 20x 5x 15x 4x 4x 1x 1x 3x 5x 5x 5x 5x 3x 2x 1x 1x 1x 1x 3x 2x 2x 1x 303x 80x 223x 191x 32x 32x 303x 303x 24x 3x 3x 3x 21x 2x 19x 1x 2x 5x 271x 191x 80x 76x 76x 4x 1x 1x 1x 3x 1x 1x 2x 78x 2x 76x 271x 2x 74x 28x 46x 241x 163x 163x 163x 235x 10x 10x 10x 10x 235x 2x 2x 2x 55x 55x 16x 39x 55x 55x 16x 39x 39x 55x 235x 189x 46x 46x 14x 32x 234x 225x 9x 9x 2x 7x 7x 7x 7x 234x 234x 241x 241x 235x 235x 235x 235x 235x 235x 235x 234x 234x 234x 11x 11x 7x 4x 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.
*/
import AssignmentKind from "./AssignmentKind";
import IAssignmentContext from "./IAssignmentContext";
import ICodeGenSymbols from "../../../types/ICodeGenSymbols";
import SubscriptClassifier from "../subscript/SubscriptClassifier";
import TTypeInfo from "../types/TTypeInfo";
import TypeCheckUtils from "../../../../utils/TypeCheckUtils";
/**
* Dependencies for classification.
*/
interface IClassifierDeps {
/** Symbol information (registers, bitmaps, structs, etc.) */
readonly symbols: ICodeGenSymbols;
/** Type registry: variable name -> type info */
readonly typeRegistry: ReadonlyMap<string, TTypeInfo>;
/** Current scope name, null if not in scope */
readonly currentScope: string | null;
/** Check if a type name is a known struct */
isKnownStruct(typeName: string): boolean;
/** Check if a name is a known scope */
isKnownScope(name: string): boolean;
/** Get member type info for a struct field */
getMemberTypeInfo(structType: string, memberName: string): TTypeInfo | null;
}
/**
* 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 {
constructor(private readonly deps: IClassifierDeps) {}
/**
* Check if typeInfo represents a simple string type (not a 2D+ string array).
*/
private 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 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.
*/
classify(ctx: IAssignmentContext): AssignmentKind {
// === Priority 1: Bitmap field assignments ===
const bitmapKind = this.classifyBitmapField(ctx);
if (bitmapKind !== null) {
return bitmapKind;
}
// === Priority 2: Member access with subscripts (arrays, register bits) ===
const memberSubscriptKind = this.classifyMemberWithSubscript(ctx);
if (memberSubscriptKind !== null) {
return memberSubscriptKind;
}
// === Priority 3: Global/this prefix patterns ===
const prefixKind = this.classifyPrefixPattern(ctx);
if (prefixKind !== null) {
return prefixKind;
}
// === Priority 4: Simple array/bit access ===
const arrayBitKind = this.classifyArrayOrBitAccess(ctx);
if (arrayBitKind !== null) {
return arrayBitKind;
}
// === Priority 5: Atomic/overflow compound assignments ===
const specialKind = this.classifySpecialCompound(ctx);
if (specialKind !== null) {
return specialKind;
}
// === Priority 6: String assignments ===
const stringKind = this.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 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 this.classifySimpleBitmapField(ids[0], ids[1]);
}
if (ids.length === 3) {
return this.classifyThreeIdBitmapField(ids[0], ids[1], ids[2]);
}
if (ids.length === 4) {
return this.classifyScopedRegisterBitmapField(ids);
}
return null;
}
/**
* Classify 2-id bitmap field: var.field
*/
private classifySimpleBitmapField(
varName: string,
fieldName: string,
): AssignmentKind | null {
const typeInfo = this.deps.typeRegistry.get(varName);
if (!typeInfo?.isBitmap || !typeInfo.bitmapTypeName) {
return null;
}
const width = this.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 classifyThreeIdBitmapField(
firstName: string,
secondName: string,
fieldName: string,
): AssignmentKind | null {
// Check if register member bitmap field: REG.MEMBER.field
if (this.deps.symbols.knownRegisters.has(firstName)) {
const bitmapType = this.lookupRegisterMemberBitmapType(
firstName,
secondName,
);
Eif (bitmapType) {
const width = this.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 = this.deps.typeRegistry.get(firstName);
if (!structTypeInfo || !this.deps.isKnownStruct(structTypeInfo.baseType)) {
return null;
}
const memberInfo = this.deps.getMemberTypeInfo(
structTypeInfo.baseType,
secondName,
);
Iif (!memberInfo) {
return null;
}
const width = this.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 classifyScopedRegisterBitmapField(
ids: readonly string[],
): AssignmentKind | null {
const scopeName = ids[0];
if (!this.deps.isKnownScope(scopeName)) {
return null;
}
const fullRegName = `${scopeName}_${ids[1]}`;
Iif (!this.deps.symbols.knownRegisters.has(fullRegName)) {
return null;
}
const bitmapType = this.lookupRegisterMemberBitmapType(fullRegName, ids[2]);
Iif (!bitmapType) {
return null;
}
const width = this.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 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 = this.deps.typeRegistry.get(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 this.classifyMultiDimArrayAccess(typeInfo, ctx.subscripts.length);
}
// 2+ identifiers with subscripts: register bit or bitmap array
Eif (ids.length >= 2) {
const registerKind = this.classifyRegisterBitAccess(
ids,
ctx.subscripts.length,
);
if (registerKind !== null) {
return registerKind;
}
return this.classifyBitmapArrayField(
ids[1],
typeInfo,
ctx.subscripts.length,
);
}
return null;
}
/**
* Classify multi-dimensional array access: arr[i][j] or arr[i][j][bit]
*/
private 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 classifyRegisterBitAccess(
ids: readonly string[],
subscriptCount: number,
): AssignmentKind | null {
const firstId = ids[0];
// Check for scoped register: Scope.REG.MEMBER[bit]
if (this.deps.isKnownScope(firstId) && ids.length >= 3) {
const scopedRegName = `${firstId}_${ids[1]}`;
Eif (this.deps.symbols.knownRegisters.has(scopedRegName)) {
return subscriptCount === 2
? AssignmentKind.REGISTER_BIT_RANGE
: AssignmentKind.REGISTER_BIT;
}
}
// Check for non-scoped register: REG.MEMBER[bit]
if (this.deps.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 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 = this.lookupBitmapFieldWidth(
typeInfo.bitmapTypeName,
secondId,
);
Eif (width !== null) {
return AssignmentKind.BITMAP_ARRAY_ELEMENT_FIELD;
}
return null;
}
/**
* Classify global.* and this.* prefix patterns.
*/
private classifyPrefixPattern(
ctx: IAssignmentContext,
): AssignmentKind | null {
if (!ctx.hasGlobal && !ctx.hasThis) {
return null;
}
if (ctx.hasGlobal && ctx.postfixOpsCount > 0) {
return this.classifyGlobalPrefix(ctx);
}
if (ctx.hasThis && ctx.postfixOpsCount > 0) {
return this.classifyThisPrefix(ctx);
}
return null;
}
/**
* Classify global.* patterns: global.reg[bit], global.arr[i], global.member
*/
private classifyGlobalPrefix(ctx: IAssignmentContext): AssignmentKind {
const firstId = ctx.identifiers[0];
if (ctx.hasArrayAccess) {
Iif (this.deps.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 classifyThisPrefix(ctx: IAssignmentContext): AssignmentKind {
Iif (!this.deps.currentScope) {
return AssignmentKind.THIS_MEMBER;
}
const firstId = ctx.identifiers[0];
const scopedRegName = `${this.deps.currentScope}_${firstId}`;
if (ctx.hasArrayAccess) {
return this.classifyThisWithArrayAccess(ctx, scopedRegName);
}
// this.REG.MEMBER.field (scoped register bitmap field)
if (
ctx.identifiers.length === 3 &&
this.deps.symbols.knownRegisters.has(scopedRegName)
) {
const bitmapType = this.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] patterns with array access.
*/
private classifyThisWithArrayAccess(
ctx: IAssignmentContext,
scopedRegName: string,
): AssignmentKind {
if (this.deps.symbols.knownRegisters.has(scopedRegName)) {
const hasBitRange = ctx.postfixOps.some((op) => op.COMMA() !== null);
return hasBitRange
? AssignmentKind.SCOPED_REGISTER_BIT_RANGE
: AssignmentKind.SCOPED_REGISTER_BIT;
}
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 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 = this.deps.typeRegistry.get(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;
Iif (
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 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 = this.deps.typeRegistry.get(id);
} else if (ctx.isSimpleThisAccess && this.deps.currentScope) {
// this.member pattern: lookup using scoped name
const memberName = ctx.identifiers[0];
const scopedName = `${this.deps.currentScope}_${memberName}`;
typeInfo = this.deps.typeRegistry.get(scopedName);
} else if (ctx.isSimpleGlobalAccess) {
// global.member pattern: lookup using direct name
const memberName = ctx.identifiers[0];
typeInfo = this.deps.typeRegistry.get(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)
// Also applies to scoped atomic variables with clamp behavior
if (
typeInfo.overflowBehavior === "clamp" &&
TypeCheckUtils.isInteger(typeInfo.baseType)
) {
return AssignmentKind.OVERFLOW_CLAMP;
}
return null;
}
/**
* Check if a simple identifier is a string variable.
*/
private _classifySimpleStringVar(
ctx: IAssignmentContext,
): AssignmentKind | null {
if (!ctx.isSimpleIdentifier) return null;
const id = ctx.identifiers[0];
const typeInfo = this.deps.typeRegistry.get(id);
return this.isSimpleStringType(typeInfo)
? AssignmentKind.STRING_SIMPLE
: null;
}
/**
* Check if this.member is a string.
*/
private _classifyThisMemberString(
ctx: IAssignmentContext,
): AssignmentKind | null {
if (!ctx.isSimpleThisAccess || !this.deps.currentScope) return null;
const memberName = ctx.identifiers[0];
const scopedName = `${this.deps.currentScope}_${memberName}`;
const typeInfo = this.deps.typeRegistry.get(scopedName);
return this.isSimpleStringType(typeInfo)
? AssignmentKind.STRING_THIS_MEMBER
: null;
}
/**
* Check if global.member is a string.
*/
private _classifyGlobalString(
ctx: IAssignmentContext,
): AssignmentKind | null {
if (!ctx.isSimpleGlobalAccess) return null;
const id = ctx.identifiers[0];
const typeInfo = this.deps.typeRegistry.get(id);
return this.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 _resolveStructType(structName: string): string | null {
const structTypeInfo = this.deps.typeRegistry.get(structName);
if (!structTypeInfo || !this.deps.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 _resolveStructFieldType(structFieldNames: {
structName: string;
fieldName: string;
}): { structType: string; fieldType: string | undefined } | null {
const structType = this._resolveStructType(structFieldNames.structName);
if (!structType) {
return null;
}
const structFields = this.deps.symbols.structFields.get(structType);
const fieldType = structFields?.get(structFieldNames.fieldName);
return { structType, fieldType };
}
/**
* Check if struct.field is a string field.
*/
private _classifyStructFieldString(
ctx: IAssignmentContext,
structFieldNames: { structName: string; fieldName: string } | null,
): AssignmentKind | null {
if (!ctx.hasMemberAccess || ctx.hasArrayAccess || !structFieldNames) {
return null;
}
const resolved = this._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 _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 = this._resolveStructFieldType(structFieldNames);
if (!resolved) {
return null;
}
const { structType, fieldType } = resolved;
const { fieldName } = structFieldNames;
const fieldArrays = this.deps.symbols.structFieldArrays.get(structType);
const dimensions = this.deps.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 classifyStringAssignment(
ctx: IAssignmentContext,
): AssignmentKind | null {
// Simple string variable
const simpleVar = this._classifySimpleStringVar(ctx);
if (simpleVar) return simpleVar;
// this.member string
const thisMember = this._classifyThisMemberString(ctx);
Iif (thisMember) return thisMember;
// global.member string
const globalMember = this._classifyGlobalString(ctx);
Iif (globalMember) return globalMember;
// struct.field or struct.arr[i] string
const structFieldNames = this.getStructFieldNames(ctx);
const structField = this._classifyStructFieldString(ctx, structFieldNames);
if (structField) return structField;
const structArrayElement = this._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 lookupBitmapFieldWidth(
bitmapTypeName: string,
fieldName: string,
): number | null {
const fields = this.deps.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 lookupRegisterMemberBitmapType(
registerName: string,
memberName: string,
): string | null {
const key = `${registerName}_${memberName}`;
return this.deps.symbols.registerMemberTypes.get(key) ?? null;
}
}
export default AssignmentClassifier;
|