All files / transpiler/output/headers BaseHeaderGenerator.ts

100% Statements 52/52
87.8% Branches 36/41
100% Functions 13/13
100% Lines 52/52

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                                      30x                                                                                   326x 18x     326x 324x           2x         2x   326x   326x                             2x   2x     2x                                                             330x     330x 378x       330x     330x         330x 330x 1x     330x                       330x           330x                   330x       22x       330x         330x 326x                 328x                                                                               330x                     328x 124x     204x 204x 262x         262x 262x     204x 204x                       262x 262x     262x     262x   262x 132x 167x   132x     262x                         167x               167x 167x         167x          
/**
 * Base Header Generator
 *
 * Abstract base class for C and C++ header generators.
 * Uses Template Method pattern - subclasses implement getRefSuffix() to
 * determine pointer (*) vs reference (&) semantics.
 */
 
import IHeaderSymbol from "./types/IHeaderSymbol";
import IParameterSymbol from "../../../utils/types/IParameterSymbol";
import IHeaderOptions from "../codegen/types/IHeaderOptions";
import IHeaderTypeInput from "./generators/IHeaderTypeInput";
import typeUtils from "./generators/mapType";
import HeaderGeneratorUtils from "./HeaderGeneratorUtils";
import SymbolTable from "../../logic/symbols/SymbolTable";
// Unified parameter generation (Phase 1)
import ParameterInputAdapter from "../codegen/helpers/ParameterInputAdapter";
import ParameterSignatureBuilder from "../codegen/helpers/ParameterSignatureBuilder";
 
const { mapType } = typeUtils;
 
/** Pass-by-value parameter info from CodeGenerator */
type TPassByValueParams = ReadonlyMap<string, ReadonlySet<string>>;
 
/**
 * Abstract base class for header file generation
 *
 * Generates header files (.h) from C-Next symbols. Subclasses implement
 * getRefSuffix() to control pass-by-reference semantics:
 * - CHeaderGenerator returns "*" for pointer-based C semantics
 * - CppHeaderGenerator returns "&" for reference-based C++ semantics
 */
abstract class BaseHeaderGenerator {
  /**
   * Reject an external type this header cannot correctly declare.
   *
   * Issue #1225/#1238: `typedef struct X X;` is a guess that only holds when X
   * really is an opaque struct. For a pointer typedef it declares a different
   * type -- and an object of it cannot be declared at all, so a header carrying
   * `extern handle_t h;` against it is not valid C or C++.
   *
   * The previous code filtered pointer typedefs out of the list silently, which
   * swapped a contradictory declaration for a missing one. Neither is a safe
   * degradation, so report it: reaching here means the type's defining header
   * was not propagated into this header, and that is a transpiler defect the
   * generated code should not paper over.
   *
   * Returns void rather than a filtered list on purpose -- an earlier version
   * looked like a filter and was not one, which invites the next reader to
   * preserve filtering that never existed.
   *
   * Normally unreachable: `Transpiler._needsDefiningHeader` sets
   * `cHeadersIncluded` for exactly these types, and the caller only invokes
   * this when it is false.
   */
  private static assertNoPointerTypedefs(
    externalTypes: string[],
    symbolTable: SymbolTable | undefined,
    headerName: string,
    symbols: IHeaderSymbol[],
  ): void {
    const pointerTypedef = externalTypes.find((typeName) =>
      symbolTable?.isPointerTypedef(typeName),
    );
 
    if (pointerTypedef === undefined) {
      return;
    }
 
    // The declaration that pulled the type in. This fires on a file the user
    // did not write, so naming the C-Next declaration is the difference
    // between an actionable error and a puzzle.
    const origin = BaseHeaderGenerator.findDeclaringSymbol(
      symbols,
      pointerTypedef,
    );
    const declaredBy =
      origin === undefined ? "" : ` It is named by '${origin.name}'.`;
    const location =
      origin?.sourceLine === undefined ? "" : ` Line ${origin.sourceLine}`;
 
    throw new Error(
      `E0505: '${pointerTypedef}' is a typedef of a pointer declared in another ` +
        `header, and generated header '${headerName}' does not include that ` +
        `header.${declaredBy} A forward declaration cannot express a pointer ` +
        `typedef -- 'typedef struct ${pointerTypedef} ${pointerTypedef};' would ` +
        `declare a different, incomplete type. The header that defines it must ` +
        `be included.${location}`,
    );
  }
 
  /** The exported symbol whose type pulled `typeName` into this header. */
  private static findDeclaringSymbol(
    symbols: IHeaderSymbol[],
    typeName: string,
  ): IHeaderSymbol | undefined {
    return symbols.find(
      (symbol) =>
        HeaderGeneratorUtils.extractBaseType(symbol.type ?? "") === typeName ||
        (symbol.parameters ?? []).some(
          (parameter) =>
            HeaderGeneratorUtils.extractBaseType(parameter.type) === typeName,
        ),
    );
  }
 
  /**
   * Get the suffix for pass-by-reference parameters
   * @returns "*" for C pointer semantics, "&" for C++ reference semantics
   */
  protected abstract getRefSuffix(): string;
 
  /**
   * Generate a header file from symbols
   *
   * @param symbols - Array of symbols to include in header
   * @param filename - Output filename (used for include guard)
   * @param options - Header generation options (includes cppMode)
   * @param typeInput - Optional type information for full definitions
   * @param passByValueParams - Map of function names to pass-by-value parameter names
   * @param allKnownEnums - All known enum names from entire compilation
   * @param sourcePath - Optional source file path for header comment
   */
  generate(
    symbols: IHeaderSymbol[],
    filename: string,
    options: IHeaderOptions = {},
    typeInput?: IHeaderTypeInput,
    passByValueParams?: TPassByValueParams,
    allKnownEnums?: ReadonlySet<string>,
    sourcePath?: string,
  ): string {
    const guard = HeaderGeneratorUtils.makeGuard(filename);
 
    // Filter to exported symbols if requested
    const exportedSymbols = options.exportedOnly
      ? symbols.filter((s) => s.isExported)
      : symbols;
 
    // Group symbols by kind
    const groups = HeaderGeneratorUtils.groupSymbolsByKind(exportedSymbols);
 
    // Get local type names for external type detection
    const localTypes = HeaderGeneratorUtils.getLocalTypeNames(groups);
 
    // Collect external type dependencies
    // #1164: typedefs this header emits itself are local. Without this the
    // header forward-declares its own callback typedef as an unknown struct.
    const localTypeNamesWithCallbacks = new Set(localTypes.localTypeNames);
    for (const [, cbInfo] of typeInput?.callbackTypes ?? []) {
      localTypeNamesWithCallbacks.add(cbInfo.typedefName);
    }
 
    const externalTypes = HeaderGeneratorUtils.collectExternalTypes(
      groups.functions,
      groups.variables,
      localTypes.localStructNames,
      localTypes.localEnumNames,
      localTypeNamesWithCallbacks,
      localTypes.localBitmapNames,
      allKnownEnums,
    );
 
    // Build external type header includes
    const { typesWithHeaders, headersToInclude } =
      HeaderGeneratorUtils.buildExternalTypeIncludes(
        externalTypes,
        options.externalTypeHeaders,
      );
 
    // Get symbol table for C++ namespace detection
    const symbolTable = typeInput?.symbolTable;
 
    // Filter to C-compatible external types
    //
    // Issue #1200: a callback type (ADR-029) is not an external struct. It is
    // emitted as a function-pointer typedef by generateCallbackTypedefSection
    // below, so forward-declaring it as `typedef struct X X;` here produces two
    // conflicting declarations of the same name -- and the name also belongs to
    // the function the type was defined from.
    const cCompatibleExternalTypes =
      HeaderGeneratorUtils.filterCCompatibleTypes(
        externalTypes,
        typesWithHeaders,
        symbolTable,
      ).filter((typeName) => !typeInput?.callbackTypes?.has(typeName));
 
    // Filter to C-compatible variables
    const cCompatibleVariables =
      HeaderGeneratorUtils.filterCCompatibleVariables(
        groups.variables,
        symbolTable,
      );
 
    if (!options.cHeadersIncluded) {
      BaseHeaderGenerator.assertNoPointerTypedefs(
        cCompatibleExternalTypes,
        symbolTable,
        filename,
        symbols,
      );
    }
 
    // Build header sections using utility methods
    const lines: string[] = [
      ...HeaderGeneratorUtils.generateHeaderStart(guard, sourcePath),
      ...HeaderGeneratorUtils.generateIncludes(options, headersToInclude),
      ...HeaderGeneratorUtils.generateCppWrapperStart(),
      ...HeaderGeneratorUtils.generateForwardDeclarations(
        // #1164: `typedef struct opaque_t* handle_t` is a different type from
        // `struct handle_t`, so the usual forward declaration contradicts the
        // real definition. Its defining header is included instead, which is
        // what cHeadersIncluded means; assertNoPointerTypedefs above has
        // already rejected the case where it was not.
        options.cHeadersIncluded ? [] : cCompatibleExternalTypes,
      ),
      ...HeaderGeneratorUtils.generateIsrTypedefSection(
        options.needsIsrTypedef ?? false,
      ),
      ...HeaderGeneratorUtils.generateEnumSection(groups.enums, typeInput),
      ...HeaderGeneratorUtils.generateBitmapSection(groups.bitmaps, typeInput),
      ...HeaderGeneratorUtils.generateTypeAliasSection(groups.types),
      ...HeaderGeneratorUtils.generateCallbackStructForwardDecls(
        groups.structs,
        typeInput,
      ),
      ...HeaderGeneratorUtils.generateCallbackTypedefSection(
        typeInput,
        options.cppMode,
      ),
      ...HeaderGeneratorUtils.generateStructSection(
        groups.structs,
        groups.classes,
        typeInput,
      ),
      ...HeaderGeneratorUtils.generateVariableSection(cCompatibleVariables),
      ...this.generateFunctionSection(
        groups.functions,
        passByValueParams,
        allKnownEnums,
      ),
      ...HeaderGeneratorUtils.generateHeaderEnd(guard),
    ];
 
    return lines.join("\n");
  }
 
  /**
   * Generate function prototypes section
   */
  private generateFunctionSection(
    functions: IHeaderSymbol[],
    passByValueParams?: TPassByValueParams,
    allKnownEnums?: ReadonlySet<string>,
  ): string[] {
    if (functions.length === 0) {
      return [];
    }
 
    const lines: string[] = ["/* Function prototypes */"];
    for (const sym of functions) {
      const proto = this.generateFunctionPrototype(
        sym,
        passByValueParams,
        allKnownEnums,
      );
      Eif (proto) {
        lines.push(proto);
      }
    }
    lines.push("");
    return lines;
  }
 
  /**
   * Generate a function prototype
   */
  private generateFunctionPrototype(
    sym: IHeaderSymbol,
    passByValueParams?: TPassByValueParams,
    allKnownEnums?: ReadonlySet<string>,
  ): string | null {
    // Map return type (main() always returns int)
    const mappedType = sym.type ? mapType(sym.type) : "void";
    const returnType = sym.name === "main" ? "int" : mappedType;
 
    // Get pass-by-value parameter names for this function
    const passByValueSet = passByValueParams?.get(sym.name);
 
    // Build parameter list
    let params = "void";
 
    if (sym.parameters && sym.parameters.length > 0) {
      const translatedParams = sym.parameters.map((p) =>
        this.generateParameter(p, passByValueSet, allKnownEnums),
      );
      params = translatedParams.join(", ");
    }
 
    return `${returnType} ${sym.name}(${params});`;
  }
 
  /**
   * Generate a single parameter with appropriate semantics
   */
  private generateParameter(
    p: IParameterSymbol,
    passByValueSet?: ReadonlySet<string>,
    allKnownEnums?: ReadonlySet<string>,
  ): string {
    // Pre-compute pass-by-value (ISR, float, enum, or explicitly marked)
    const isPassByValue =
      p.type === "ISR" ||
      p.type === "f32" ||
      p.type === "f64" ||
      allKnownEnums?.has(p.type) ||
      passByValueSet?.has(p.name) ||
      false;
 
    // Build normalized input using adapter
    const input = ParameterInputAdapter.fromSymbol(p, {
      mapType: (t) => mapType(t),
      isPassByValue,
    });
 
    // Use shared builder with subclass-specific ref suffix
    return ParameterSignatureBuilder.build(input, this.getRefSuffix());
  }
}
 
export default BaseHeaderGenerator;