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 | 357x 357x 357x 6x 351x 43x 308x 8x 300x 300x 300x 300x 300x 3x 300x 300x 357x 214x 214x 21x 193x 214x 3x 190x 1x 189x 189x 189x 6x 43x 43x 1x 43x 43x 7x 43x 21x 21x 21x 21x 351x 351x 200x 351x 8x 8x 8x 8x | /**
* ParameterInputAdapter - Adapts different input formats to IParameterInput
*
* Provides two conversion methods:
* - fromAST(): For CodeGenerator, converts IPlannedParameter + CodeGenState
* - fromSymbol(): For HeaderGenerator, converts IParameterSymbol
*
* Both produce normalized IParameterInput for use with ParameterSignatureBuilder.
*
* #1445: `fromAST` keeps its name and takes a record of facts now rather than
* a `ParameterContext` and three callbacks that each took another parse node.
* Everything those callbacks did was turn a node into a string; every decision
* after that is about strings and `CodeGenState`, which is why this module
* names no parse type at all.
*
* The two methods therefore differ in WHICH record they read, not in kind --
* which is what makes the remaining problem tractable. They are still two
* derivations of one parameter, from two sources, and the .c/.h divergences
* this file keeps recording (#914, #1164, #1545) are three instances of that
* one defect rather than three bugs. Filed as #1639.
*/
import AdrProvenance from "../../../../instrumentation/AdrProvenance";
import IParameterInput from "../types/IParameterInput";
import type IPlannedParameter from "../types/IPlannedParameter";
import IParameterSymbol from "../../../../utils/types/IParameterSymbol";
import ICallbackTypeInfo from "../../../../types/ICallbackTypeInfo";
import AutoConstRule from "../../../../utils/AutoConstRule";
/**
* 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 {
/** 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;
/**
* #1545: Whether a type name is a known enum. ADR-013 passes enums by value,
* so they take no auto-const. The header path already excluded them and this
* path did not; AutoConstRule now holds that decision for both, which is why
* the fact has to reach here.
*/
isKnownEnum: (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;
/**
* Issue #995 / #1722: whether this parameter is an opaque handle (an
* incomplete struct typedef). Pre-computed, like `isPassByValue`: it is the
* parameter registry's answer (`TParameterInfo.isOpaqueHandle`), so the
* signature and every whole-value use of the parameter read one decision.
* An opaque handle takes pointer syntax and no auto-const, because the C
* APIs it is passed to expect a mutable pointer.
*/
isOpaqueHandle: 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(
planned: IPlannedParameter,
deps: IFromASTDeps,
): IParameterInput {
const { name, typeName, mappedType, isConst } = planned;
// Check for callback type
const callbackInfo = deps.callbackTypes.get(typeName);
if (callbackInfo) {
return this._buildCallbackInput(
name,
typeName,
mappedType,
callbackInfo.typedefName,
);
}
// Check for array type
if (planned.renderDimensions) {
return this._buildArrayInputFromAST(planned, deps);
}
// Check for string type (non-array)
if (planned.isString) {
return this._buildStringInput(planned, deps);
}
// Determine classification for non-array, non-string types
const isKnownStruct = deps.isKnownStruct(typeName);
const isKnownPrimitive = !!deps.typeMap[typeName];
// Issue #995: Opaque handles — rule applied in ParameterSignatureBuilder.
// #1722: read, not re-derived; see IFromASTDeps.isOpaqueHandle. It is also
// #958's "C-header typedef struct needs pointer semantics": that asked
// `isTypedefStructType`, which StructCollector marks under exactly the
// condition it marks an opaque type, so it was the same fact decided a
// second time.
const isOpaque = deps.isOpaqueHandle;
const isTypedefStruct = isOpaque;
if (isOpaque) {
// ADR-030 decided here: an incomplete type can only be handled through a
// pointer, which is why #995's `const T*` was wrong. Recorded at the
// PARAMETER's position so the matrix sees the enclosing function's
// context.
//
// #1511: this is the only thing an occupancy can be derived from for the
// OPAQUE-HANDLE half of ADR-030, which shapes generated code and reports
// nothing. The ADR does raise diagnostics -- E0422/E0423/E0426/E0427 --
// and since #1582 their fixtures occupy cells here too, on their own
// reported positions. Occupancy is per ADR, not per code, so a cell
// occupied by one half says nothing about the other: deleting this line
// leaves `scope method / same file` green.
//
// #1445: the position comes from the planned parameter now. It is the
// same position -- `ctx.start?.line` of the parameter context -- read
// once by the planner instead of here.
AdrProvenance.record("030", planned.line);
}
const isAutoConst = this._autoConst(
typeName,
isConst,
false,
deps,
planned.line,
);
// 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,
// Issue #995: Pass through opaque handle detection — rule applied in builder
isOpaqueHandle: isOpaque || undefined,
};
}
/**
* 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,
// #1545: the general branch below carries this and the string branch
// did not, so a typedef declaring `const char *` reached the .c (via
// forceConst on the AST path) and never reached the .h -- the same
// .c/.h disagreement as the missing callback term, in the opposite
// direction.
forceConst: param.isCallbackConst || undefined,
};
}
// ADR-029 / #1164: a parameter whose type IS a callback function is written
// as its typedef, with no added pointer — the typedef is already a function
// pointer. Hardcoding isCallback false here made the header emit
// "onReceive_fp* handler" where the .c emits "onReceive_fp handler".
if (param.isCallback && param.callbackTypedefName) {
return {
name: param.name,
baseType: param.type,
mappedType,
isConst: param.isConst,
isAutoConst: false,
isArray: false,
isCallback: true,
callbackTypedefName: param.callbackTypedefName,
isString: false,
isPassByValue: true,
isPassByReference: false,
};
}
// Issue #914: Callback typedef overrides — param carries resolved pointer/const
// info. This is deliberately tri-state (TypedefParamParser.isParamPointer
// returns boolean | null): true means the typedef takes a pointer, FALSE
// means it takes the value, and undefined means there is no typedef to
// follow. Collapsing false into undefined with `?? false` sent a by-value
// typedef back through ADR-006 reference semantics, so `void (*)(Point)`
// got a `Point*` prototype against a `Point` definition (#1164).
const callbackWantsPointer = param.isCallbackPointer;
const callbackWantsValue = callbackWantsPointer === false;
return {
name: param.name,
baseType: param.type,
mappedType,
isConst: param.isConst,
isAutoConst: param.isAutoConst ?? false,
isArray: false,
isCallback: false,
isString: false,
isPassByValue: callbackWantsPointer
? false
: callbackWantsValue || deps.isPassByValue,
isPassByReference: callbackWantsPointer
? true
: !callbackWantsValue && !deps.isPassByValue,
forcePointerSyntax: callbackWantsPointer || undefined,
forceConst: param.isCallbackConst || undefined,
// Issue #995: Pass through opaque handle detection — rule applied in builder
isOpaqueHandle: param.isOpaqueHandle || 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(
planned: IPlannedParameter,
deps: IFromASTDeps,
): IParameterInput {
const { name, typeName, mappedType, isConst, isString } = planned;
// ADR-030 / #996 decided here for an array of handles, exactly as the
// scalar branch records it for one handle: the element is held through a
// pointer. Recorded at the parameter's position, like that branch.
if (deps.isOpaqueHandle) {
AdrProvenance.record("030", planned.line);
}
// Issue #1159 is decided by the planner: a dimension that is a
// compile-time constant is folded to its value, because emitting the
// identifier makes `u8[SIZE] buf` a VLA parameter while the matching local
// declaration folds to `uint8_t b[6]` -- the same const rendered two ways
// in one .c, and a construct CLAUDE.md rules out.
const dims: string[] = [...planned.renderDimensions!()];
// The null terminator is decided HERE, not by the planner: `string<8>[2]`
// is two 9-byte rows, and a capacity is a language fact where a dimension
// is a C one.
if (isString && planned.stringCapacity !== undefined) {
dims.push(String(planned.stringCapacity + 1));
}
// ADR-006: Arrays are pass-by-reference and mutable by default, so the rule
// returns false for every array. It is asked rather than hardcoded so the
// array policy has ONE spelling: #1602 is open on ADR-013 still listing
// arrays as receiving auto-const, and whoever resolves it must not have to
// find this branch as well.
return {
name,
baseType: typeName,
mappedType,
isConst,
isAutoConst: this._autoConst(
typeName,
isConst,
true,
deps,
planned.arrayTypeLine,
),
isArray: true,
arrayDimensions: dims,
isCallback: false,
isString,
isPassByValue: false, // Arrays are always passed by pointer
isPassByReference: false,
// #1545: an array parameter drops the typedef's const exactly as the
// string branch did. A `void (*)(const uint8_t *)` typedef against
// `void onData(u8[4] buf)` emitted `uint8_t buf[4]` in BOTH files, so the
// two agreed with each other and neither matched the typedef --
// an incompatible-pointer-type warning at the registration, transpiler exit 0.
forceConst: deps.forceConst,
// ADR-030 / #996: the registry's one answer, which for an array means
// each element is a handle -- `Dev* arr[2]`, not an array of an
// incomplete type.
isOpaqueHandle: deps.isOpaqueHandle || undefined,
};
}
/**
* 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,
// #1545: same as the AST array branch -- the typedef's const has to reach
// the header too, or the .h contradicts the .c it was generated beside.
forceConst: param.isCallbackConst || undefined,
// ADR-030 / #996: same as the AST array branch -- an array of handles is
// an array of pointers in the header too.
isOpaqueHandle: param.isOpaqueHandle || undefined,
};
}
/**
* #1545: ADR-013 auto-const for a parse-tree parameter.
*
* One place that translates this file's vocabulary into IAutoConstFacts, so
* a fact added to the rule cannot reach two of the three branches and miss
* the third -- which is how the general path, the string path and the header
* path came to hold three different rules in the first place.
*/
private static _autoConst(
typeName: string,
isConst: boolean,
isArray: boolean,
deps: IFromASTDeps,
line: number | undefined,
): boolean {
const applies = AutoConstRule.applies({
baseType: typeName,
isModified: deps.isModified,
isExplicitlyConst: isConst,
isCallbackCompatible: deps.isCallbackCompatible,
isArray,
isKnownEnum: deps.isKnownEnum(typeName),
// #995: supplied here rather than at the three call sites, so the string,
// array and general branches cannot disagree about it the way they
// disagreed about the callback term. #1722: the parameter's one answer,
// which is false for an array -- where the rule has already refused.
isOpaqueHandle: deps.isOpaqueHandle,
});
// Recorded where the rule FIRED, which is what #1241 derives occupancy
// from, and the shape ADR-030 uses eleven lines up (`if (isOpaque)`). An
// unguarded record at the top of fromAST credited ADR-013 for every
// parameter of every function -- two of the three positions it produced
// for this branch's own fixture were callback parameters the rule had just
// refused. ADR-013's codegen half raises no diagnostic, so a provenance
// site is the only thing its occupancy can be derived from.
if (applies) {
AdrProvenance.record("013", line);
}
return applies;
}
/**
* Build IParameterInput for a non-array string parameter.
*/
private static _buildStringInput(
planned: IPlannedParameter,
deps: IFromASTDeps,
): IParameterInput {
const { name, typeName, isConst } = planned;
const capacity = planned.stringCapacity;
// #1545: the same rule the general path uses. This line previously omitted
// the callback term, so a callback-compatible function's unmodified string
// parameter took `const char*` in the .c while the .h kept `char*` -- the
// definition contradicting its own prototype.
const isAutoConst = this._autoConst(
typeName,
isConst,
false,
deps,
planned.stringTypeLine,
);
return {
name,
baseType: typeName,
mappedType: "char",
isConst,
isAutoConst,
isArray: false,
isCallback: false,
isString: true,
stringCapacity: capacity,
isPassByValue: false,
isPassByReference: false,
// #1545: the third site that dropped the typedef's const. The general
// branch of fromAST carries this and the string branch did not, so a
// `const char *` typedef reached neither file once auto-const stopped
// supplying the const by accident. Found by the fixture, not by reading.
forceConst: deps.forceConst,
};
}
}
export default ParameterInputAdapter;
|