All files / transpiler/logic/symbols/cnext/adapters TSymbolInfoAdapter.ts

97.76% Statements 175/179
93.87% Branches 46/49
96% Functions 24/25
98.28% Lines 172/175

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                                                                                                                                                              1471x 1471x 1471x 1471x 1471x     1471x 1471x       1471x     1471x 1471x 1471x           1471x     1471x       1471x 1471x     1471x 1471x 1471x 1471x 1471x 1471x     1471x     1471x         1471x     1471x 2964x   239x             239x     109x 109x     30x             30x     277x           277x     22x                 22x       756x         756x       1531x 1531x                   1471x             1471x                                                                                             2x             1471x           30x                   239x 239x   239x 239x 239x   239x   404x 404x   404x 19x   19x         19x         239x 239x 239x 19x                               1471x     1471x 2964x 2725x   239x 239x 239x 220x   19x 19x     20x                                   109x 109x 109x                   30x 30x 30x 30x   30x 30x 367x         30x                 277x       277x 277x     277x               22x 22x 22x     22x 22x 10x     22x 60x   60x 60x 60x           60x 33x                   756x 756x 756x     756x 133x 133x 7x 7x   133x       756x     756x             3x                     1531x 1531x 1531x       60x                   2x 2x   2x 2x                         19x 19x     19x                         19x 19x 4x   19x                           80x 17x                                     14x   120x 30x 27x                             20x 20x 20x           20x             20x   20x         20x         20x       20x       20x         20x                                                                           312x 312x 18x 18x 18x   312x               20x 1x       19x 19x 19x 19x 19x 1x   19x 19x 19x 19x 19x         19x 20x                             19x                                                               11x 1x       10x 10x 11x       10x          
/**
 * TSymbolInfoAdapter - Converts TSymbol[] to ISymbolInfo for CodeGenerator.
 *
 * ADR-055 Phase 5: This adapter enables CodeGenerator to use pre-collected
 * symbols from CNextResolver instead of creating its own SymbolCollector.
 *
 * The conversion extracts and restructures the rich discriminated union types
 * into the flat map format that CodeGenerator expects via ISymbolInfo.
 */
 
import ICodeGenSymbols from "../../../../types/ICodeGenSymbols";
import CNEXT_TO_C_TYPE_MAP from "../../../../../utils/constants/TypeMappings";
import TSymbol from "../../../../types/symbols/TSymbol";
import IBitmapSymbol from "../../../../types/symbols/IBitmapSymbol";
import IEnumSymbol from "../../../../types/symbols/IEnumSymbol";
import IFunctionSymbol from "../../../../types/symbols/IFunctionSymbol";
import IStructSymbol from "../../../../types/symbols/IStructSymbol";
import IRegisterSymbol from "../../../../types/symbols/IRegisterSymbol";
import IScopeSymbol from "../../../../types/symbols/IScopeSymbol";
import IVariableSymbol from "../../../../types/symbols/IVariableSymbol";
import TypeResolver from "../../../../../utils/TypeResolver";
import ScopeUtils from "../../../../../utils/ScopeUtils";
import QualifiedCName from "../../../../../utils/QualifiedCName";
import PublicInterface from "../../PublicInterface";
 
/**
 * Groups register-related maps for processRegister method.
 * Reduces parameter count for SonarCloud compliance.
 */
interface IRegisterMaps {
  knownRegisters: Set<string>;
  scopedRegisters: Map<string, string>;
  registerMemberAccess: Map<string, string>;
  registerMemberTypes: Map<string, string>;
  registerBaseAddresses: Map<string, string>;
  registerMemberOffsets: Map<string, string>;
  registerMemberCTypes: Map<string, string>;
}
 
/**
 * Converts TSymbol[] to ISymbolInfo for CodeGenerator.
 * Replaces the need for SymbolCollector during code generation.
 */
/**
 * The mutable collections mergeExternalSymbols accumulates into.
 *
 * Grouped into one object rather than passed as eight parameters: adding the
 * three type-forming sets for #1333 took the parameter list past what is
 * readable, and a positional list of eight same-shaped collections is a
 * transposition waiting to happen.
 */
interface IMergeAccumulator {
  readonly knownEnums: Set<string>;
  readonly knownScopes: Set<string>;
  readonly enumMembers: Map<string, Map<string, number>>;
  readonly functionReturnTypes: Map<string, string>;
  readonly scopeMemberVisibility: Map<
    string,
    Map<string, "public" | "private">
  >;
  readonly knownStructs: Set<string>;
  readonly knownBitmaps: Set<string>;
  readonly bitmapFields: Map<
    string,
    Map<string, { readonly offset: number; readonly width: number }>
  >;
  readonly bitmapBackingType: Map<string, string>;
  readonly bitmapBitWidth: Map<string, number>;
}
 
class TSymbolInfoAdapter {
  /**
   * Convert TSymbol[] to ISymbolInfo for CodeGenerator consumption.
   *
   * @param symbols Array of discriminated union symbols from CNextResolver
   * @returns ISymbolInfo compatible with CodeGenerator
   */
  static convert(symbols: readonly TSymbol[]): ICodeGenSymbols {
    // === Known Type Sets ===
    const knownScopes = new Set<string>();
    const knownStructs = new Set<string>();
    const knownEnums = new Set<string>();
    const knownBitmaps = new Set<string>();
    const knownRegisters = new Set<string>();
 
    // === Scope Information ===
    const scopeMembers = new Map<string, Set<string>>();
    const scopeMemberVisibility = new Map<
      string,
      Map<string, "public" | "private">
    >();
    const scopeVariableUsage = new Map<string, Set<string>>();
 
    // === Struct Information ===
    const structFields = new Map<string, Map<string, string>>();
    const structFieldArrays = new Map<string, Set<string>>();
    const structFieldDimensions = new Map<
      string,
      Map<string, (number | string)[]>
    >();
 
    // === Enum Information ===
    const enumMembers = new Map<string, Map<string, number>>();
 
    // === Bitmap Information ===
    const bitmapFields = new Map<
      string,
      Map<string, { offset: number; width: number }>
    >();
    const bitmapBackingType = new Map<string, string>();
    const bitmapBitWidth = new Map<string, number>();
 
    // === Register Information ===
    const scopedRegisters = new Map<string, string>();
    const registerMemberAccess = new Map<string, string>();
    const registerMemberTypes = new Map<string, string>();
    const registerBaseAddresses = new Map<string, string>();
    const registerMemberOffsets = new Map<string, string>();
    const registerMemberCTypes = new Map<string, string>();
 
    // === Issue #282: Private const values for inlining ===
    const scopePrivateConstValues = new Map<string, string>();
 
    // === Function Return Types ===
    const functionReturnTypes = new Map<string, string>();
 
    // === Issue #948: Opaque Types ===
    // Note: Opaque types are populated from SymbolTable, not TSymbol[]
    // This will be an empty set here; actual values come from Transpiler
    const opaqueTypes = new Set<string>();
 
    // Process each symbol
    for (const symbol of symbols) {
      switch (symbol.kind) {
        case "struct":
          TSymbolInfoAdapter.processStruct(
            symbol,
            knownStructs,
            structFields,
            structFieldArrays,
            structFieldDimensions,
          );
          break;
 
        case "enum":
          TSymbolInfoAdapter.processEnum(symbol, knownEnums, enumMembers);
          break;
 
        case "bitmap":
          TSymbolInfoAdapter.processBitmap(
            symbol,
            knownBitmaps,
            bitmapFields,
            bitmapBackingType,
            bitmapBitWidth,
          );
          break;
 
        case "scope":
          TSymbolInfoAdapter.processScope(
            symbol,
            knownScopes,
            scopeMembers,
            scopeMemberVisibility,
          );
          break;
 
        case "register":
          TSymbolInfoAdapter.processRegister(symbol, knownBitmaps, {
            knownRegisters,
            scopedRegisters,
            registerMemberAccess,
            registerMemberTypes,
            registerBaseAddresses,
            registerMemberOffsets,
            registerMemberCTypes,
          });
          break;
 
        case "variable":
          // Track scope membership and private const values
          TSymbolInfoAdapter.processVariable(
            symbol,
            scopeMembers,
            scopePrivateConstValues,
          );
          break;
 
        // Track function return types for enum validation
        case "function":
          TSymbolInfoAdapter.processFunction(symbol, functionReturnTypes);
          break;
      }
    }
 
    // Issue #1127: qualify struct field dimensions that name a symbol.
    //
    // A second pass, not inline in processStruct: structs and enums are
    // processed by one loop in symbol order, so knownEnums is still being
    // filled while structs are read. Qualifying inline would make the result
    // depend on whether the enum happens to be declared above the struct.
    TSymbolInfoAdapter.qualifyStructFieldDimensions(
      symbols,
      structFieldDimensions,
      knownEnums,
    );
 
    // Build the ISymbolInfo result
    const result: ICodeGenSymbols = {
      // Type sets
      knownScopes,
      knownStructs,
      knownEnums,
      knownBitmaps,
      knownRegisters,
 
      // Scope info
      scopeMembers,
      scopeMemberVisibility,
      scopeVariableUsage,
 
      // Struct info
      structFields,
      structFieldArrays,
      structFieldDimensions,
 
      // Enum info
      enumMembers,
 
      // Bitmap info
      bitmapFields,
      bitmapBackingType,
      bitmapBitWidth,
 
      // Register info
      scopedRegisters,
      registerMemberAccess,
      registerMemberTypes,
      registerBaseAddresses,
      registerMemberOffsets,
      registerMemberCTypes,
 
      // Issue #282: Private const values for inlining
      scopePrivateConstValues,
 
      // Function return types
      functionReturnTypes,
 
      // Issue #948: Opaque types
      opaqueTypes,
 
      // Methods
      hasPublicInterface: PublicInterface.existsIn(symbols),
 
      getSingleFunctionForVariable: (scopeName: string, varName: string) =>
        TSymbolInfoAdapter.getSingleFunctionForVariable(
          scopeVariableUsage,
          scopeName,
          varName,
        ),
    };
 
    return result;
  }
 
  // === Private Processing Methods ===
 
  // Use shared utility for transpiled C names
  private static readonly getTranspiledCName = ScopeUtils.getTranspiledCName;
 
  private static processStruct(
    struct: IStructSymbol,
    knownStructs: Set<string>,
    structFields: Map<string, Map<string, string>>,
    structFieldArrays: Map<string, Set<string>>,
    structFieldDimensions: Map<string, Map<string, (number | string)[]>>,
  ): void {
    // Use transpiled C name for lookups (e.g., "Geometry_Point")
    const cName = TSymbolInfoAdapter.getTranspiledCName(struct);
    knownStructs.add(cName);
 
    const fields = new Map<string, string>();
    const arrayFields = new Set<string>();
    const dimensions = new Map<string, (number | string)[]>();
 
    for (const [fieldName, fieldInfo] of struct.fields) {
      // Convert TType to string for legacy ISymbolInfo format
      const typeStr = TypeResolver.getTypeName(fieldInfo.type);
      fields.set(fieldName, typeStr);
 
      if (fieldInfo.isArray) {
        arrayFields.add(fieldName);
 
        Eif (fieldInfo.dimensions && fieldInfo.dimensions.length > 0) {
          // Issue #1127: keep non-numeric dimensions. Filtering them out
          // dropped enum-qualified counts, so `u8[EColor.COUNT] slots` reached
          // the header as a scalar and the body as a bit-indexed value.
          // Filtering also shifted any dimension that followed a dropped one.
          dimensions.set(fieldName, [...fieldInfo.dimensions]);
        }
      }
    }
 
    structFields.set(cName, fields);
    structFieldArrays.set(cName, arrayFields);
    if (dimensions.size > 0) {
      structFieldDimensions.set(cName, dimensions);
    }
  }
 
  /**
   * Resolve struct field dimensions that name a symbol to their C identifier.
   *
   * Runs after every symbol has been seen, so `knownEnums` is complete and the
   * answer does not depend on declaration order. Numeric dimensions and plain
   * macro names pass through untouched.
   */
  private static qualifyStructFieldDimensions(
    symbols: readonly TSymbol[],
    structFieldDimensions: Map<string, Map<string, (number | string)[]>>,
    knownEnums: ReadonlySet<string>,
  ): void {
    const isKnownEnum = (qualifiedName: string): boolean =>
      knownEnums.has(qualifiedName);
 
    for (const symbol of symbols) {
      if (symbol.kind !== "struct") {
        continue;
      }
      const cName = TSymbolInfoAdapter.getTranspiledCName(symbol);
      const fieldDimensions = structFieldDimensions.get(cName);
      if (!fieldDimensions) {
        continue;
      }
      for (const [fieldName, dimensions] of fieldDimensions) {
        fieldDimensions.set(
          fieldName,
          dimensions.map((dimension) =>
            typeof dimension === "string"
              ? ScopeUtils.resolveDimensionName(
                  dimension,
                  symbol.scope,
                  isKnownEnum,
                )
              : dimension,
          ),
        );
      }
    }
  }
 
  private static processEnum(
    enumSym: IEnumSymbol,
    knownEnums: Set<string>,
    enumMembers: Map<string, Map<string, number>>,
  ): void {
    const cName = TSymbolInfoAdapter.getTranspiledCName(enumSym);
    knownEnums.add(cName);
    enumMembers.set(cName, new Map(enumSym.members));
  }
 
  private static processBitmap(
    bitmap: IBitmapSymbol,
    knownBitmaps: Set<string>,
    bitmapFields: Map<string, Map<string, { offset: number; width: number }>>,
    bitmapBackingType: Map<string, string>,
    bitmapBitWidth: Map<string, number>,
  ): void {
    const cName = TSymbolInfoAdapter.getTranspiledCName(bitmap);
    knownBitmaps.add(cName);
    bitmapBackingType.set(cName, bitmap.backingType);
    bitmapBitWidth.set(cName, bitmap.bitWidth);
 
    const fields = new Map<string, { offset: number; width: number }>();
    for (const [fieldName, fieldInfo] of bitmap.fields) {
      fields.set(fieldName, {
        offset: fieldInfo.offset,
        width: fieldInfo.width,
      });
    }
    bitmapFields.set(cName, fields);
  }
 
  private static processScope(
    scope: IScopeSymbol,
    knownScopes: Set<string>,
    scopeMembers: Map<string, Set<string>>,
    scopeMemberVisibility: Map<string, Map<string, "public" | "private">>,
  ): void {
    knownScopes.add(scope.name);
 
    // Use scope.members as the authoritative list of member names
    // This includes functions, variables, enums, structs, etc.
    const members = new Set<string>(scope.members);
    scopeMembers.set(scope.name, members);
 
    // Copy visibility map
    scopeMemberVisibility.set(scope.name, new Map(scope.memberVisibility));
  }
 
  private static processRegister(
    register: IRegisterSymbol,
    knownBitmaps: Set<string>,
    maps: IRegisterMaps,
  ): void {
    const cName = TSymbolInfoAdapter.getTranspiledCName(register);
    maps.knownRegisters.add(cName);
    maps.registerBaseAddresses.set(cName, register.baseAddress);
 
    // Check if this is a scoped register (has non-global scope)
    const isScoped = register.scope.name !== "";
    if (isScoped) {
      maps.scopedRegisters.set(cName, register.baseAddress);
    }
 
    for (const [memberName, memberInfo] of register.members) {
      const fullName = QualifiedCName.fromParts([cName, memberName]);
 
      maps.registerMemberAccess.set(fullName, memberInfo.access);
      maps.registerMemberOffsets.set(fullName, memberInfo.offset);
      maps.registerMemberCTypes.set(
        fullName,
        TSymbolInfoAdapter.cnextTypeToCType(memberInfo.cType),
      );
 
      // Track bitmap types for register members
      if (memberInfo.bitmapType && knownBitmaps.has(memberInfo.bitmapType)) {
        maps.registerMemberTypes.set(fullName, memberInfo.bitmapType);
      }
    }
  }
 
  private static processVariable(
    variable: IVariableSymbol,
    scopeMembers: Map<string, Set<string>>,
    scopePrivateConstValues: Map<string, string>,
  ): void {
    const cName = TSymbolInfoAdapter.getTranspiledCName(variable);
    const scopeName = variable.scope.name;
    const isScoped = scopeName !== "";
 
    // Track scoped variables as scope members (needed for name resolution)
    if (isScoped) {
      let members = scopeMembers.get(scopeName);
      if (!members) {
        members = new Set<string>();
        scopeMembers.set(scopeName, members);
      }
      members.add(variable.name); // Add local name (e.g., "value"), not transpiled C name
    }
 
    // Issue #282: Track private const values for inlining
    const isPrivate = !variable.isExported;
 
    // Issue #500: Only inline SCALAR consts, not arrays - arrays must be emitted
    if (
      isScoped &&
      isPrivate &&
      variable.isConst &&
      variable.initialValue &&
      !variable.isArray
    ) {
      scopePrivateConstValues.set(cName, variable.initialValue);
    }
  }
 
  private static processFunction(
    func: IFunctionSymbol,
    functionReturnTypes: Map<string, string>,
  ): void {
    // Track function return types for enum validation in assignments
    // This enables recognizing that Motor.getMode() returns Motor_EMode
    // Use transpiled C name (e.g., "Motor_getMode") for lookup consistency
    const cName = TSymbolInfoAdapter.getTranspiledCName(func);
    const returnTypeStr = TypeResolver.getTypeName(func.returnType);
    functionReturnTypes.set(cName, returnTypeStr);
  }
 
  private static cnextTypeToCType(typeName: string): string {
    return CNEXT_TO_C_TYPE_MAP[typeName] || typeName;
  }
 
  private static getSingleFunctionForVariable(
    scopeVariableUsage: Map<string, Set<string>>,
    scopeName: string,
    varName: string,
  ): string | null {
    // #1295: scopeVariableUsage is populated with leaf-built keys, so this
    // lookup matches it. Both move together or neither does.
    const fullVarName = QualifiedCName.fromParts([scopeName, varName]);
    const usedIn = scopeVariableUsage.get(fullVarName);
 
    Eif (usedIn?.size !== 1) {
      return null;
    }
 
    // Extract the single element from the Set (we know it exists since size === 1)
    return [...usedIn][0];
  }
 
  /**
   * Create a deep copy of enum members map
   */
  private static _copyEnumMembers(
    enumMembers: ReadonlyMap<string, ReadonlyMap<string, number>>,
  ): Map<string, Map<string, number>> {
    const copy = new Map<string, Map<string, number>>();
    for (const [enumName, members] of enumMembers) {
      copy.set(enumName, new Map(members));
    }
    return copy;
  }
 
  /**
   * Deep-copy a scopeName -> (memberName -> visibility) map so the merged
   * result never aliases the base's inner maps.
   */
  private static _copyScopeMemberVisibility(
    scopeMemberVisibility: ReadonlyMap<
      string,
      ReadonlyMap<string, "public" | "private">
    >,
  ): Map<string, Map<string, "public" | "private">> {
    const copy = new Map<string, Map<string, "public" | "private">>();
    for (const [scopeName, visibility] of scopeMemberVisibility) {
      copy.set(scopeName, new Map(visibility));
    }
    return copy;
  }
 
  /**
   * Merge a single external source into the merged data structures
   */
  /**
   * Add every member of `from` into `into`. A name-only set: no precedence
   * question arises because the sets carry no payload.
   */
  private static _mergeNames(
    from: ReadonlySet<string>,
    into: Set<string>,
  ): void {
    for (const name of from) {
      into.add(name);
    }
  }
 
  /**
   * Merge `from` into `into`, **local wins**: an entry already present is never
   * overwritten by an external one.
   *
   * The nine merges in _mergeExternalSource were nine copies of this loop, which
   * is both the duplication and the cognitive complexity SonarCloud flagged
   * (S3776, 22 against 15). More to the point, "local takes precedence" was
   * restated nine times, so a tenth merge could silently choose otherwise.
   *
   * `clone` exists because half the values are Maps that must not be aliased
   * between the base and the merged result.
   */
  private static _mergePreferringLocal<K, V>(
    from: ReadonlyMap<K, V>,
    into: Map<K, V>,
    clone: (value: V) => V = (value) => value,
  ): void {
    for (const [key, value] of from) {
      if (!into.has(key)) {
        into.set(key, clone(value));
      }
    }
  }
 
  private static _mergeExternalSource(
    external: ICodeGenSymbols,
    into: IMergeAccumulator,
  ): void {
    // #1333: every type-forming kind crosses the include boundary on the same
    // terms. Only knownEnums did, so ADR-057 qualification was kind-dependent: in
    // a scope spanning two files, an enum declared in the other file qualified and
    // a struct did not. Adjacent lines in one function emitted `Lib__Mode m` and
    // bare `Point p` -- the second does not compile. The asymmetry was invisible
    // while a scope could not span files at all, which is the bug this shipped with.
    TSymbolInfoAdapter._mergeNames(external.knownEnums, into.knownEnums);
    TSymbolInfoAdapter._mergeNames(external.knownStructs, into.knownStructs);
    TSymbolInfoAdapter._mergeNames(external.knownBitmaps, into.knownBitmaps);
 
    // Issue #1190: the visibility map travels with the scope name. Registering a
    // scope as known while leaving its visibility unknown makes every member of an
    // included scope look public, because the access check reads `undefined` and
    // only rejects an explicit "private".
    TSymbolInfoAdapter._mergeNames(external.knownScopes, into.knownScopes);
 
    // A type's NAME is not enough; its detail travels with it. enumMembers already
    // moved with knownEnums, which is exactly why enums were the only kind that
    // ever worked -- carrying knownBitmaps alone let a cross-file bitmap type
    // resolve and then hard-error on the field behind it ("Unknown bitmap field
    // 'Mode' on type 'Lib__Flags'"). Same asymmetry, one level down.
    const cloneMap = <K, V>(m: ReadonlyMap<K, V>): Map<K, V> => new Map(m);
 
    TSymbolInfoAdapter._mergePreferringLocal(
      external.enumMembers,
      into.enumMembers,
      cloneMap,
    );
    TSymbolInfoAdapter._mergePreferringLocal(
      external.bitmapFields,
      into.bitmapFields,
      cloneMap,
    );
    TSymbolInfoAdapter._mergePreferringLocal(
      external.bitmapBackingType,
      into.bitmapBackingType,
    );
    TSymbolInfoAdapter._mergePreferringLocal(
      external.bitmapBitWidth,
      into.bitmapBitWidth,
    );
    TSymbolInfoAdapter._mergePreferringLocal(
      external.scopeMemberVisibility,
      into.scopeMemberVisibility,
      cloneMap,
    );
    TSymbolInfoAdapter._mergePreferringLocal(
      external.functionReturnTypes,
      into.functionReturnTypes,
    );
  }
 
  /**
   * Issue #465: Merge external symbol info into an existing ISymbolInfo.
   *
   * When a file includes other .cnx files, the enum types and scopes from those
   * external files need to be available for code generation. This enables:
   * - Enum member prefixing for external enums
   * - Cross-scope method calls like global.Scope.method() returning enums
   * - Visibility enforcement on members of included scopes (#1190)
   *
   * This method creates a new ISymbolInfo that includes both the base symbols
   * and merged info from external sources.
   *
   * @param base The ISymbolInfo from the current file
   * @param externalSources Array of ISymbolInfo from included .cnx files
   * @returns New ISymbolInfo with merged enum, scope and visibility data
   */
  /**
   * The qualified names of every type-forming declaration in these sources.
   *
   * #1333: fed to CNextResolver Pass 0b so the symbols layer sees the same scope
   * types the codegen layer does. The two resolve type names independently
   * (CLAUDE.md, "Two resolution points, one decision"), and a reopened scope puts
   * half its members in another file -- so without this the `.h` prototype and the
   * `.c` definition disagree about a parameter's type and gcc rejects the pair.
   *
   * Kinds match CodeGenState.isScopeType: enums, structs and bitmaps. Registers
   * are excluded, exactly as they are there -- a register declares a variable at
   * an address, not a type.
   */
  static collectScopeTypeNames(
    sources: readonly ICodeGenSymbols[],
  ): ReadonlySet<string> {
    const names = new Set<string>();
    for (const source of sources) {
      for (const name of source.knownEnums) names.add(name);
      for (const name of source.knownStructs) names.add(name);
      for (const name of source.knownBitmaps) names.add(name);
    }
    return names;
  }
 
  static mergeExternalSymbols(
    base: ICodeGenSymbols,
    externalSources: ICodeGenSymbols[],
  ): ICodeGenSymbols {
    // If no external sources, return base unchanged
    if (externalSources.length === 0) {
      return base;
    }
 
    // Create mutable copies of enum-related data and scope info
    const mergedKnownEnums = new Set(base.knownEnums);
    const mergedKnownScopes = new Set(base.knownScopes);
    const mergedKnownStructs = new Set(base.knownStructs);
    const mergedKnownBitmaps = new Set(base.knownBitmaps);
    const mergedBitmapFields = new Map(
      [...base.bitmapFields].map(([name, fields]) => [name, new Map(fields)]),
    );
    const mergedBitmapBackingType = new Map(base.bitmapBackingType);
    const mergedBitmapBitWidth = new Map(base.bitmapBitWidth);
    const mergedEnumMembers = this._copyEnumMembers(base.enumMembers);
    const mergedFunctionReturnTypes = new Map(base.functionReturnTypes);
    const mergedScopeMemberVisibility = this._copyScopeMemberVisibility(
      base.scopeMemberVisibility,
    );
 
    // Merge in external enum info, function return types, scopes and visibility
    for (const external of externalSources) {
      this._mergeExternalSource(external, {
        knownEnums: mergedKnownEnums,
        knownScopes: mergedKnownScopes,
        enumMembers: mergedEnumMembers,
        functionReturnTypes: mergedFunctionReturnTypes,
        scopeMemberVisibility: mergedScopeMemberVisibility,
        knownStructs: mergedKnownStructs,
        knownBitmaps: mergedKnownBitmaps,
        bitmapFields: mergedBitmapFields,
        bitmapBackingType: mergedBitmapBackingType,
        bitmapBitWidth: mergedBitmapBitWidth,
      });
    }
 
    // Return new ICodeGenSymbols with merged enum data and scope info
    return {
      ...base,
      knownScopes: mergedKnownScopes,
      knownEnums: mergedKnownEnums,
      knownStructs: mergedKnownStructs,
      knownBitmaps: mergedKnownBitmaps,
      bitmapFields: mergedBitmapFields,
      bitmapBackingType: mergedBitmapBackingType,
      bitmapBitWidth: mergedBitmapBitWidth,
      enumMembers: mergedEnumMembers,
      functionReturnTypes: mergedFunctionReturnTypes,
      scopeMemberVisibility: mergedScopeMemberVisibility,
    };
  }
 
  /**
   * Issue #948: Merge opaque types from an external source (e.g., SymbolTable)
   * into an existing ICodeGenSymbols.
   *
   * Opaque types are forward-declared struct types (like `typedef struct _foo foo;`)
   * that come from C headers and need to be tracked for correct scope variable
   * generation (as pointers with NULL initialization).
   *
   * @param base The ICodeGenSymbols from the current file
   * @param externalOpaqueTypes Array of opaque type names from external sources
   * @returns New ICodeGenSymbols with merged opaque types
   */
  static mergeOpaqueTypes(
    base: ICodeGenSymbols,
    externalOpaqueTypes: string[],
  ): ICodeGenSymbols {
    // If no external opaque types, return base unchanged
    if (externalOpaqueTypes.length === 0) {
      return base;
    }
 
    // Create merged set with existing and external opaque types
    const mergedOpaqueTypes = new Set(base.opaqueTypes);
    for (const typeName of externalOpaqueTypes) {
      mergedOpaqueTypes.add(typeName);
    }
 
    // Return new ICodeGenSymbols with merged opaque types
    return { ...base, opaqueTypes: mergedOpaqueTypes };
  }
}
 
export default TSymbolInfoAdapter;