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

97.95% Statements 144/147
87.5% Branches 42/48
100% Functions 18/18
97.95% Lines 144/147

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                                                                                                    1139x 1139x 1139x 1139x 1139x     1139x 1139x       1139x     1139x 1139x 1139x     1139x     1139x       1139x 1139x     1139x 1139x 1139x 1139x 1139x 1139x     1139x     1139x         1139x     1139x 2133x   174x             174x     99x 99x     18x             18x     187x           187x     14x                 14x       560x         560x       1081x 1081x         1139x                                                                                         2x             962x     1139x                   962x 149x 204x 134x       828x             14x                   174x 174x   174x 174x 174x   174x   281x 281x   281x 18x   18x   18x 19x   18x 18x           174x 174x 174x 18x                 99x 99x 99x                   18x 18x 18x 18x   18x 18x 85x         18x                 187x       187x 187x     187x               14x 14x 14x     14x 14x 2x     14x 12x   12x 12x 12x           12x 1x                   560x 560x 560x     560x 85x 85x 5x 5x   85x       560x     560x             3x                     1081x 1081x 1081x       12x               2x 2x   2x 2x                         9x 9x     9x                           9x 2x     9x 4x     9x 2x 2x       9x 3x 3x                                                 9x         9x 9x 9x 9x     9x 9x                   9x                                                   7x 1x       6x 6x 7x       6x          
/**
 * 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 SymbolNameUtils from "../utils/SymbolNameUtils";
 
/**
 * 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.
 */
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: 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[]>>();
 
    // === 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;
      }
    }
 
    // 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
      getSingleFunctionForVariable: (scopeName: string, varName: string) =>
        TSymbolInfoAdapter.getSingleFunctionForVariable(
          scopeVariableUsage,
          scopeName,
          varName,
        ),
 
      hasPublicSymbols: () =>
        TSymbolInfoAdapter.checkHasPublicSymbols(scopeMemberVisibility),
    };
 
    return result;
  }
 
  /**
   * Check if any scope members are public (exported).
   * Used to determine if a self-include header is needed for extern "C" linkage.
   */
  private static checkHasPublicSymbols(
    scopeMemberVisibility: Map<string, Map<string, "public" | "private">>,
  ): boolean {
    for (const [, visibilityMap] of scopeMemberVisibility) {
      for (const [, visibility] of visibilityMap) {
        if (visibility === "public") {
          return true;
        }
      }
    }
    return false;
  }
 
  // === Private Processing Methods ===
 
  // Use shared utility for transpiled C names
  private static readonly getTranspiledCName =
    SymbolNameUtils.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[]>>,
  ): 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[]>();
 
    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) {
          // Filter to only include numeric dimensions
          const numericDims = fieldInfo.dimensions.filter(
            (d): d is number => typeof d === "number",
          );
          Eif (numericDims.length > 0) {
            dimensions.set(fieldName, numericDims);
          }
        }
      }
    }
 
    structFields.set(cName, fields);
    structFieldArrays.set(cName, arrayFields);
    if (dimensions.size > 0) {
      structFieldDimensions.set(cName, dimensions);
    }
  }
 
  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 = `${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 {
    const fullVarName = `${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;
  }
 
  /**
   * Merge a single external source into the merged data structures
   */
  private static _mergeExternalSource(
    external: ICodeGenSymbols,
    mergedKnownEnums: Set<string>,
    mergedKnownScopes: Set<string>,
    mergedEnumMembers: Map<string, Map<string, number>>,
    mergedFunctionReturnTypes: Map<string, string>,
  ): void {
    // Merge known enums
    for (const enumName of external.knownEnums) {
      mergedKnownEnums.add(enumName);
    }
    // Merge scopes from external sources for cross-scope method calls
    for (const scopeName of external.knownScopes) {
      mergedKnownScopes.add(scopeName);
    }
    // Merge enum members (local takes precedence)
    for (const [enumName, members] of external.enumMembers) {
      Eif (!mergedEnumMembers.has(enumName)) {
        mergedEnumMembers.set(enumName, new Map(members));
      }
    }
    // Merge function return types (local takes precedence)
    for (const [funcName, returnType] of external.functionReturnTypes) {
      Eif (!mergedFunctionReturnTypes.has(funcName)) {
        mergedFunctionReturnTypes.set(funcName, returnType);
      }
    }
  }
 
  /**
   * 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
   *
   * 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 externalEnumSources Array of ISymbolInfo from included .cnx files
   * @returns New ISymbolInfo with merged enum and scope data
   */
  static mergeExternalEnums(
    base: ICodeGenSymbols,
    externalEnumSources: ICodeGenSymbols[],
  ): ICodeGenSymbols {
    // If no external sources, return base unchanged
    Iif (externalEnumSources.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 mergedEnumMembers = this._copyEnumMembers(base.enumMembers);
    const mergedFunctionReturnTypes = new Map(base.functionReturnTypes);
 
    // Merge in external enum info, function return types, and scopes
    for (const external of externalEnumSources) {
      this._mergeExternalSource(
        external,
        mergedKnownEnums,
        mergedKnownScopes,
        mergedEnumMembers,
        mergedFunctionReturnTypes,
      );
    }
 
    // Return new ICodeGenSymbols with merged enum data and scope info
    return {
      ...base,
      knownScopes: mergedKnownScopes,
      knownEnums: mergedKnownEnums,
      enumMembers: mergedEnumMembers,
      functionReturnTypes: mergedFunctionReturnTypes,
    };
  }
 
  /**
   * 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;