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

97.47% Statements 116/119
87.5% Branches 35/40
100% Functions 16/16
97.47% Lines 116/119

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                                                                                                  1012x 1012x 1012x 1012x 1012x     1012x 1012x       1012x     1012x 1012x 1012x     1012x     1012x       1012x 1012x     1012x 1012x 1012x 1012x 1012x 1012x     1012x     1012x     1012x 1901x   144x             144x     95x 95x     15x             15x     159x           159x     12x                 12x       524x 524x       952x 952x         1012x                                                                                   2x             846x     1012x                   846x 126x 181x 111x       735x                       144x   144x 144x 144x   144x 235x   235x 17x   17x 17x         144x 144x 144x 17x                 95x 95x                   15x 15x 15x   15x 15x 74x         15x                 159x     159x     159x               12x 12x     12x 1x     12x 10x   10x 10x 10x           10x 1x                     524x 524x     524x             3x                   952x       10x               2x 2x   2x 2x                         8x 8x     8x                           8x 2x     8x 4x     8x 2x 2x       8x 2x 2x                                                 8x         8x 8x 8x 8x     8x 8x                     8x                                                                                                
/**
 * 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 ESymbolKind from "../../../../../utils/types/ESymbolKind";
import TSymbol from "../../types/TSymbol";
import IBitmapSymbol from "../../types/IBitmapSymbol";
import IEnumSymbol from "../../types/IEnumSymbol";
import IFunctionSymbol from "../../types/IFunctionSymbol";
import IStructSymbol from "../../types/IStructSymbol";
import IRegisterSymbol from "../../types/IRegisterSymbol";
import IScopeSymbol from "../../types/IScopeSymbol";
import IVariableSymbol from "../../types/IVariableSymbol";
 
/**
 * 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>();
 
    // Process each symbol
    for (const symbol of symbols) {
      switch (symbol.kind) {
        case ESymbolKind.Struct:
          TSymbolInfoAdapter.processStruct(
            symbol,
            knownStructs,
            structFields,
            structFieldArrays,
            structFieldDimensions,
          );
          break;
 
        case ESymbolKind.Enum:
          TSymbolInfoAdapter.processEnum(symbol, knownEnums, enumMembers);
          break;
 
        case ESymbolKind.Bitmap:
          TSymbolInfoAdapter.processBitmap(
            symbol,
            knownBitmaps,
            bitmapFields,
            bitmapBackingType,
            bitmapBitWidth,
          );
          break;
 
        case ESymbolKind.Namespace:
          TSymbolInfoAdapter.processScope(
            symbol,
            knownScopes,
            scopeMembers,
            scopeMemberVisibility,
          );
          break;
 
        case ESymbolKind.Register:
          TSymbolInfoAdapter.processRegister(symbol, knownBitmaps, {
            knownRegisters,
            scopedRegisters,
            registerMemberAccess,
            registerMemberTypes,
            registerBaseAddresses,
            registerMemberOffsets,
            registerMemberCTypes,
          });
          break;
 
        case ESymbolKind.Variable:
          // Issue #282: Track private const values for inlining
          TSymbolInfoAdapter.processVariable(symbol, scopePrivateConstValues);
          break;
 
        // Track function return types for enum validation
        case ESymbolKind.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,
 
      // 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 ===
 
  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 {
    knownStructs.add(struct.name);
 
    const fields = new Map<string, string>();
    const arrayFields = new Set<string>();
    const dimensions = new Map<string, number[]>();
 
    for (const [fieldName, fieldInfo] of struct.fields) {
      fields.set(fieldName, fieldInfo.type);
 
      if (fieldInfo.isArray) {
        arrayFields.add(fieldName);
 
        Eif (fieldInfo.dimensions && fieldInfo.dimensions.length > 0) {
          dimensions.set(fieldName, fieldInfo.dimensions);
        }
      }
    }
 
    structFields.set(struct.name, fields);
    structFieldArrays.set(struct.name, arrayFields);
    if (dimensions.size > 0) {
      structFieldDimensions.set(struct.name, dimensions);
    }
  }
 
  private static processEnum(
    enumSym: IEnumSymbol,
    knownEnums: Set<string>,
    enumMembers: Map<string, Map<string, number>>,
  ): void {
    knownEnums.add(enumSym.name);
    enumMembers.set(enumSym.name, 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 {
    knownBitmaps.add(bitmap.name);
    bitmapBackingType.set(bitmap.name, bitmap.backingType);
    bitmapBitWidth.set(bitmap.name, 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(bitmap.name, 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);
 
    // Convert members array to Set
    scopeMembers.set(scope.name, new Set(scope.members));
 
    // Copy visibility map
    scopeMemberVisibility.set(scope.name, new Map(scope.memberVisibility));
  }
 
  private static processRegister(
    register: IRegisterSymbol,
    knownBitmaps: Set<string>,
    maps: IRegisterMaps,
  ): void {
    maps.knownRegisters.add(register.name);
    maps.registerBaseAddresses.set(register.name, register.baseAddress);
 
    // Check if this is a scoped register (name contains underscore)
    if (register.name.includes("_")) {
      maps.scopedRegisters.set(register.name, register.baseAddress);
    }
 
    for (const [memberName, memberInfo] of register.members) {
      const fullName = `${register.name}_${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,
    scopePrivateConstValues: Map<string, string>,
  ): void {
    // Issue #282: Track private const values for inlining
    // A scoped variable has an underscore in its name (e.g., "Motor_MAX_SPEED")
    const isScoped = variable.name.includes("_");
    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(variable.name, 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
    functionReturnTypes.set(func.name, func.returnType);
  }
 
  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 ISymbolInfo with merged enum data and scope info
    // All other fields remain unchanged from base
    return {
      // Type sets - knownEnums and knownScopes are merged
      knownScopes: mergedKnownScopes,
      knownStructs: base.knownStructs,
      knownEnums: mergedKnownEnums,
      knownBitmaps: base.knownBitmaps,
      knownRegisters: base.knownRegisters,
 
      // Scope info
      scopeMembers: base.scopeMembers,
      scopeMemberVisibility: base.scopeMemberVisibility,
      scopeVariableUsage: base.scopeVariableUsage,
 
      // Struct info
      structFields: base.structFields,
      structFieldArrays: base.structFieldArrays,
      structFieldDimensions: base.structFieldDimensions,
 
      // Enum info - merged
      enumMembers: mergedEnumMembers,
 
      // Bitmap info
      bitmapFields: base.bitmapFields,
      bitmapBackingType: base.bitmapBackingType,
      bitmapBitWidth: base.bitmapBitWidth,
 
      // Register info
      scopedRegisters: base.scopedRegisters,
      registerMemberAccess: base.registerMemberAccess,
      registerMemberTypes: base.registerMemberTypes,
      registerBaseAddresses: base.registerBaseAddresses,
      registerMemberOffsets: base.registerMemberOffsets,
      registerMemberCTypes: base.registerMemberCTypes,
 
      // Private const values
      scopePrivateConstValues: base.scopePrivateConstValues,
 
      // Function return types - merged
      functionReturnTypes: mergedFunctionReturnTypes,
 
      // Methods - delegate to base's implementation
      getSingleFunctionForVariable: base.getSingleFunctionForVariable,
      hasPublicSymbols: base.hasPublicSymbols,
    };
  }
}
 
export default TSymbolInfoAdapter;