All files / transpiler/logic/symbols/cpp/utils DeclaratorUtils.ts

75% Statements 72/96
59.45% Branches 44/74
100% Functions 10/10
75% Lines 72/96

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                                      45x 45x 45x 45x 45x           45x                     71x 71x 45x 45x 45x 45x 45x 45x 45x             26x 71x 26x                   21x 21x 21x 21x 10x       11x 21x       11x             16x     16x 16x 16x 16x 16x   16x       16x         16x 16x 12x       4x 16x       4x                     5x 5x       5x 5x     5x 5x     5x       5x                           7x 7x   7x 7x 3x     4x       4x                                                         50x   50x 52x 52x 50x 50x 33x 33x 31x           50x                 10x 10x 10x 10x 10x         10x                         10x          
/**
 * DeclaratorUtils - Shared utilities for extracting information from C++ declarators.
 *
 * Provides methods for extracting names, types, parameters, and array dimensions
 * from C++ parse tree declarator contexts.
 */
 
/* eslint-disable @typescript-eslint/no-explicit-any */
 
import SymbolUtils from "../../SymbolUtils";
import IExtractedParameter from "../../shared/IExtractedParameter";
import ParameterExtractorUtils from "../../shared/ParameterExtractorUtils";
 
class DeclaratorUtils {
  /**
   * Extract name from a declarator context.
   */
  static extractDeclaratorName(declarator: any): string | null {
    // Pointer declarator -> noPointerDeclarator
    const ptrDecl = declarator.pointerDeclarator?.();
    Eif (ptrDecl) {
      const noPtr = ptrDecl.noPointerDeclarator?.();
      Eif (noPtr) {
        return DeclaratorUtils.extractNoPointerDeclaratorName(noPtr);
      }
    }
 
    // No pointer declarator
    const noPtr = declarator.noPointerDeclarator?.();
    Iif (noPtr) {
      return DeclaratorUtils.extractNoPointerDeclaratorName(noPtr);
    }
 
    return null;
  }
 
  /**
   * Extract name from a noPointerDeclarator context.
   */
  static extractNoPointerDeclaratorName(noPtr: any): string | null {
    const declId = noPtr.declaratorid?.();
    if (declId) {
      const idExpr = declId.idExpression?.();
      Eif (idExpr) {
        const unqualId = idExpr.unqualifiedId?.();
        Eif (unqualId) {
          const identifier = unqualId.Identifier?.();
          Eif (identifier) {
            return identifier.getText();
          }
        }
      }
    }
 
    // Recursive case
    const innerNoPtr = noPtr.noPointerDeclarator?.();
    if (innerNoPtr) {
      return DeclaratorUtils.extractNoPointerDeclaratorName(innerNoPtr);
    }
 
    return null;
  }
 
  /**
   * Check if a declarator represents a function.
   */
  static declaratorIsFunction(declarator: any): boolean {
    const ptrDecl = declarator.pointerDeclarator?.();
    Eif (ptrDecl) {
      const noPtr = ptrDecl.noPointerDeclarator?.();
      if (noPtr?.parametersAndQualifiers?.()) {
        return true;
      }
    }
 
    const noPtr = declarator.noPointerDeclarator?.();
    Iif (noPtr?.parametersAndQualifiers?.()) {
      return true;
    }
 
    return false;
  }
 
  /**
   * Extract function parameters from a declarator.
   */
  static extractFunctionParameters(declarator: any): IExtractedParameter[] {
    const params: IExtractedParameter[] = [];
 
    // Find parametersAndQualifiers from the declarator
    let paramsAndQuals: any = null;
    const ptrDecl = declarator.pointerDeclarator?.();
    Eif (ptrDecl) {
      const noPtr = ptrDecl.noPointerDeclarator?.();
      paramsAndQuals = noPtr?.parametersAndQualifiers?.();
    }
    Iif (!paramsAndQuals) {
      const noPtr = declarator.noPointerDeclarator?.();
      paramsAndQuals = noPtr?.parametersAndQualifiers?.();
    }
    Iif (!paramsAndQuals) {
      return params;
    }
 
    // Get parameterDeclarationClause
    const paramClause = paramsAndQuals.parameterDeclarationClause?.();
    if (!paramClause) {
      return params;
    }
 
    // Get parameterDeclarationList
    const paramList = paramClause.parameterDeclarationList?.();
    Iif (!paramList) {
      return params;
    }
 
    return ParameterExtractorUtils.processParameterList(
      paramList,
      DeclaratorUtils.extractParameterInfo,
    );
  }
 
  /**
   * Extract parameter info from a single parameter declaration.
   */
  static extractParameterInfo(paramDecl: any): IExtractedParameter | null {
    // Get the type from declSpecifierSeq
    const declSpecSeq = paramDecl.declSpecifierSeq?.();
    Iif (!declSpecSeq) {
      return null;
    }
 
    const baseType = DeclaratorUtils.extractTypeFromDeclSpecSeq(declSpecSeq);
    const isConst = declSpecSeq.getText().includes("const");
 
    // Check for pointer in declarator or abstractDeclarator
    const declarator = paramDecl.declarator?.();
    const abstractDecl = paramDecl.abstractDeclarator?.();
 
    const isPointer =
      (declarator && DeclaratorUtils.declaratorHasPointer(declarator)) ||
      (abstractDecl &&
        DeclaratorUtils.abstractDeclaratorHasPointer(abstractDecl));
 
    return ParameterExtractorUtils.buildParameterInfo(
      declarator,
      baseType,
      isConst,
      isPointer ?? false,
      false, // isArray - could be enhanced
      DeclaratorUtils.extractDeclaratorName,
    );
  }
 
  /**
   * Check if a declarator contains a pointer operator.
   */
  static declaratorHasPointer(declarator: any): boolean {
    const ptrDecl = declarator.pointerDeclarator?.();
    Eif (ptrDecl) {
      // Check for pointerOperator children
      const ptrOps = ptrDecl.pointerOperator?.();
      if (ptrOps && ptrOps.length > 0) {
        return true;
      }
      // Also check getText for *
      Iif (ptrDecl.getText().includes("*")) {
        return true;
      }
    }
    return false;
  }
 
  /**
   * Check if an abstract declarator contains a pointer.
   */
  static abstractDeclaratorHasPointer(abstractDecl: any): boolean {
    // Check for pointerAbstractDeclarator
    const ptrAbstract = abstractDecl.pointerAbstractDeclarator?.();
    if (ptrAbstract) {
      const ptrOps = ptrAbstract.pointerOperator?.();
      if (ptrOps && ptrOps.length > 0) {
        return true;
      }
      if (ptrAbstract.getText().includes("*")) {
        return true;
      }
    }
    // Simple check for * in the text
    if (abstractDecl.getText().includes("*")) {
      return true;
    }
    return false;
  }
 
  /**
   * Extract type string from a declSpecifierSeq context.
   */
  static extractTypeFromDeclSpecSeq(declSpecSeq: any): string {
    const parts: string[] = [];
 
    for (const spec of declSpecSeq.declSpecifier?.() ?? []) {
      const typeSpec = spec.typeSpecifier?.();
      if (typeSpec) {
        const trailingType = typeSpec.trailingTypeSpecifier?.();
        if (trailingType) {
          const simpleType = trailingType.simpleTypeSpecifier?.();
          if (simpleType) {
            parts.push(simpleType.getText());
          }
        }
      }
    }
 
    return parts.join(" ") || "int";
  }
 
  /**
   * Extract array dimensions from a declarator.
   * Issue #981: Returns (number | string)[] to support macro-sized arrays.
   */
  static extractArrayDimensions(declarator: any): (number | string)[] {
    // For C++, we need to check both pointer and no-pointer declarators
    const ptrDecl = declarator.pointerDeclarator?.();
    Eif (ptrDecl) {
      const noPtr = ptrDecl.noPointerDeclarator?.();
      Eif (noPtr) {
        return DeclaratorUtils.extractArrayDimensionsFromNoPtr(noPtr);
      }
    }
 
    const noPtr = declarator.noPointerDeclarator?.();
    Iif (noPtr) {
      return DeclaratorUtils.extractArrayDimensionsFromNoPtr(noPtr);
    }
 
    return [];
  }
 
  /**
   * Extract array dimensions from a noPointerDeclarator.
   * Issue #981: Returns (number | string)[] to support macro-sized arrays.
   */
  static extractArrayDimensionsFromNoPtr(noPtr: any): (number | string)[] {
    // Use shared utility for regex-based extraction
    return SymbolUtils.parseArrayDimensions(noPtr.getText());
  }
}
 
export default DeclaratorUtils;