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 | 255x 255x 255x 255x 255x 255x 4x 251x 251x 34x 217x 217x 7x 210x 210x 210x 210x 255x 255x 103x 103x 15x 88x 103x 1x 87x 103x 4x 34x 34x 39x 39x 34x 34x 34x 4x 4x 4x 4x 34x 34x 15x 15x 15x 15x 7x 7x 7x 7x | /**
* ParameterInputAdapter - Adapts different input formats to IParameterInput
*
* Provides two conversion methods:
* - fromAST(): For CodeGenerator, converts Parser.ParameterContext + CodeGenState
* - fromSymbol(): For HeaderGenerator, converts IParameterSymbol
*
* Both produce normalized IParameterInput for use with ParameterSignatureBuilder.
*/
import * as Parser from "../../../logic/parser/grammar/CNextParser";
import IParameterInput from "../types/IParameterInput";
import IParameterSymbol from "../../../../utils/types/IParameterSymbol";
import ICallbackTypeInfo from "../types/ICallbackTypeInfo";
/**
* Dependencies required by fromAST() to resolve types and state.
* These are passed in to avoid direct dependency on CodeGenState,
* making the adapter more testable.
*/
interface IFromASTDeps {
/** Get C-Next type name from type context (e.g., 'u32', 'Point') */
getTypeName: (type: Parser.TypeContext) => string;
/** Generate C type from type context (e.g., 'uint32_t', 'Point') */
generateType: (type: Parser.TypeContext) => string;
/** Generate expression string (for array dimension expressions) */
generateExpression: (expr: Parser.ExpressionContext) => string;
/** Map of callback type names to their info */
callbackTypes: ReadonlyMap<string, ICallbackTypeInfo>;
/** Check if type is a known struct (C-Next or C header) */
isKnownStruct: (typeName: string) => boolean;
/** TYPE_MAP for primitive detection */
typeMap: Record<string, string>;
/** Whether the parameter is modified in the current function */
isModified: boolean;
/** Whether the parameter should use pass-by-value (pre-computed) */
isPassByValue: boolean;
/** Issue #895: Whether the current function is callback-compatible */
isCallbackCompatible: boolean;
/**
* Issue #895: Force pass-by-reference for callback-compatible functions
* When the typedef signature requires a pointer, this overrides normal logic.
*/
forcePassByReference?: boolean;
/** Issue #958: Check if a type name is a typedef'd struct from C headers */
isTypedefStructType: (typeName: string) => boolean;
/**
* Issue #895: Force const qualifier from callback typedef signature.
* When the C typedef has `const T*`, this preserves const on the generated param.
*/
forceConst?: boolean;
}
/**
* Dependencies required by fromSymbol() to resolve types.
* Simpler than AST deps since IParameterSymbol already contains most info.
*
* The caller (BaseHeaderGenerator) pre-computes isPassByValue including
* ISR/float/enum/passByValueSet checks. The adapter trusts this decision.
*/
interface IFromSymbolDeps {
/** Map C-Next type to C type */
mapType: (type: string) => string;
/** Whether the parameter should use pass-by-value (pre-computed by caller) */
isPassByValue: boolean;
}
/**
* Static adapter class for converting different input formats to IParameterInput.
*/
class ParameterInputAdapter {
/**
* Convert AST ParameterContext to normalized IParameterInput.
* Used by CodeGenerator.generateParameter().
*
* Note: Validation (C-style array rejection, unbounded dimension rejection)
* should be done BEFORE calling this method.
*
* @param ctx - The parser context for the parameter
* @param deps - Dependencies for type resolution and state lookup
* @returns Normalized IParameterInput
*/
static fromAST(
ctx: Parser.ParameterContext,
deps: IFromASTDeps,
): IParameterInput {
const isConst = ctx.constModifier() !== null;
const typeName = deps.getTypeName(ctx.type());
const name = ctx.IDENTIFIER().getText();
const mappedType = deps.generateType(ctx.type());
// Check for callback type
const callbackInfo = deps.callbackTypes.get(typeName);
if (callbackInfo) {
return this._buildCallbackInput(
name,
typeName,
mappedType,
callbackInfo.typedefName,
);
}
// Check for array type
const arrayTypeCtx = ctx.type().arrayType();
if (arrayTypeCtx) {
return this._buildArrayInputFromAST(
arrayTypeCtx,
name,
typeName,
mappedType,
isConst,
deps,
);
}
// Check for string type (non-array)
const stringTypeCtx = ctx.type().stringType();
if (stringTypeCtx) {
return this._buildStringInput(
name,
typeName,
isConst,
deps,
stringTypeCtx,
);
}
// Determine classification for non-array, non-string types
const isKnownStruct = deps.isKnownStruct(typeName);
const isKnownPrimitive = !!deps.typeMap[typeName];
// Issue #958: C-header typedef struct types need pointer semantics
const isTypedefStruct = deps.isTypedefStructType(typeName);
// Issue #895: Don't add auto-const for callback-compatible functions
// because it would change the signature and break typedef compatibility
const isAutoConst =
!deps.isCallbackCompatible && !deps.isModified && !isConst;
// Issue #895/#958: Force pass-by-reference for callback or typedef struct types
const isPassByReference =
deps.forcePassByReference ||
isKnownStruct ||
isKnownPrimitive ||
isTypedefStruct;
return {
name,
baseType: typeName,
mappedType,
isConst,
isAutoConst,
isArray: false,
isCallback: false,
isString: false,
isPassByValue: deps.isPassByValue,
isPassByReference,
// Issue #895/#958: Force pointer syntax in C++ mode for callback-compatible
// and typedef struct params (C types expect pointers, not C++ references)
forcePointerSyntax:
deps.forcePassByReference || isTypedefStruct || undefined,
// Issue #895: Preserve const from callback typedef signature
forceConst: deps.forceConst,
};
}
/**
* Convert IParameterSymbol to normalized IParameterInput.
* Used by BaseHeaderGenerator.generateParameter().
*
* The caller pre-computes isPassByValue (ISR, float, enum, passByValueSet).
* Non-PBV, non-array, non-string types use pass-by-reference.
*
* @param param - The parameter symbol
* @param deps - Dependencies for type mapping
* @returns Normalized IParameterInput
*/
static fromSymbol(
param: IParameterSymbol,
deps: IFromSymbolDeps,
): IParameterInput {
const mappedType = deps.mapType(param.type);
// Array parameters
if (
param.isArray &&
param.arrayDimensions &&
param.arrayDimensions.length > 0
) {
return this._buildArrayInputFromSymbol(param, mappedType);
}
// String type detection
const isString =
param.type === "string" || param.type.startsWith("string<");
// Non-array string
if (isString && !param.isArray) {
return {
name: param.name,
baseType: param.type,
mappedType: "char",
isConst: param.isConst,
isAutoConst: param.isAutoConst ?? false,
isArray: false,
isCallback: false,
isString: true,
isPassByValue: false,
isPassByReference: false,
};
}
// Issue #914: Callback typedef overrides — param carries resolved pointer/const info
const isCallbackPointer = param.isCallbackPointer ?? false;
return {
name: param.name,
baseType: param.type,
mappedType,
isConst: param.isConst,
isAutoConst: param.isAutoConst ?? false,
isArray: false,
isCallback: false,
isString: false,
isPassByValue: isCallbackPointer ? false : deps.isPassByValue,
isPassByReference: isCallbackPointer ? true : !deps.isPassByValue,
forcePointerSyntax: isCallbackPointer || undefined,
forceConst: param.isCallbackConst || undefined,
};
}
/**
* Build IParameterInput for a callback parameter.
*/
private static _buildCallbackInput(
name: string,
typeName: string,
mappedType: string,
typedefName: string,
): IParameterInput {
return {
name,
baseType: typeName,
mappedType,
isConst: false,
isAutoConst: false,
isArray: false,
isCallback: true,
callbackTypedefName: typedefName,
isString: false,
isPassByValue: true, // Callbacks are function pointers, pass by value
isPassByReference: false,
};
}
/**
* Build IParameterInput for an array parameter from AST.
*/
private static _buildArrayInputFromAST(
arrayTypeCtx: Parser.ArrayTypeContext,
name: string,
typeName: string,
mappedType: string,
isConst: boolean,
deps: IFromASTDeps,
): IParameterInput {
const allDims = arrayTypeCtx.arrayTypeDimension();
// Build dimension strings
const dims: string[] = allDims.map(
(d: Parser.ArrayTypeDimensionContext) => {
const expr = d.expression();
return expr ? deps.generateExpression(expr) : "";
},
);
// Check for string array (string<N>[M])
const stringTypeCtx = arrayTypeCtx.stringType();
const isString = stringTypeCtx !== null;
if (isString && stringTypeCtx) {
const intLiteral = stringTypeCtx.INTEGER_LITERAL();
Eif (intLiteral) {
const capacity = Number.parseInt(intLiteral.getText(), 10);
dims.push(String(capacity + 1));
}
}
const isAutoConst = !deps.isModified && !isConst;
return {
name,
baseType: typeName,
mappedType,
isConst,
isAutoConst,
isArray: true,
arrayDimensions: dims,
isCallback: false,
isString,
isPassByValue: false, // Arrays are always passed by pointer
isPassByReference: false,
};
}
/**
* Build IParameterInput for an array parameter from symbol.
*/
private static _buildArrayInputFromSymbol(
param: IParameterSymbol,
mappedType: string,
): IParameterInput {
const isString =
param.type === "string" || param.type.startsWith("string<");
const isUnboundedString = param.type === "string"; // No capacity specified
// For header generator, we need to use char for string arrays
const actualMappedType = isString ? "char" : mappedType;
return {
name: param.name,
baseType: param.type,
mappedType: actualMappedType,
isConst: param.isConst,
isAutoConst: param.isAutoConst ?? false,
isArray: true,
arrayDimensions: param.arrayDimensions,
isCallback: false,
isString,
isUnboundedString,
isPassByValue: false,
isPassByReference: false,
};
}
/**
* Build IParameterInput for a non-array string parameter.
*/
private static _buildStringInput(
name: string,
typeName: string,
isConst: boolean,
deps: IFromASTDeps,
stringTypeCtx: Parser.StringTypeContext,
): IParameterInput {
const intLiteral = stringTypeCtx.INTEGER_LITERAL();
const capacity = intLiteral
? Number.parseInt(intLiteral.getText(), 10)
: undefined;
const isAutoConst = !deps.isModified && !isConst;
return {
name,
baseType: typeName,
mappedType: "char",
isConst,
isAutoConst,
isArray: false,
isCallback: false,
isString: true,
stringCapacity: capacity,
isPassByValue: false,
isPassByReference: false,
};
}
}
export default ParameterInputAdapter;
|