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 | 14x 51x 44x 7x 7x 3x 2x 2x 5x 14x 14x 14x 8x 6x 6x 2x 2x 1x 6x 14x 2x 6x 14x 70x 68x 68x 68x 68x 70x 70x 14x 122x 122x 34x 88x 88x 88x 7x 81x 68x 68x 80x 80x 84x 84x 84x 14x 70x 37x 37x 33x 80x 14x 7x 2x 5x 5x 1x 4x 4x 1x 3x 3x 1x 2x 2x 2x 2x 2x 2x 7x 7x 14x 68x 68x 64x 64x 67x 67x 2x 2x 1x 14x 67x 70x 70x 38x 10x 7x | /**
* Function Call Expression Generator (ADR-053 A2 Phase 5)
*
* Generates C code for function calls:
* - safe_div/safe_mod built-in functions (ADR-051)
* - C-Next function calls with pass-by-reference semantics
* - C function calls with pass-by-value semantics
* - Const-to-non-const validation (ADR-013)
*/
import {
ArgumentListContext,
ExpressionContext,
} from "../../../../logic/parser/grammar/CNextParser";
import IGeneratorOutput from "../IGeneratorOutput";
import TGeneratorEffect from "../TGeneratorEffect";
import IGeneratorInput from "../IGeneratorInput";
import IGeneratorState from "../IGeneratorState";
import IOrchestrator from "../IOrchestrator";
import CallExprUtils from "./CallExprUtils";
/**
* Issue #304: Wrap argument with static_cast if it's a C++ enum class
* being passed to an integer parameter.
*
* @param argCode - The generated argument code
* @param argExpr - The argument expression context (for type lookup)
* @param targetParamBaseType - The target parameter's base type (if known)
* @param orchestrator - Orchestrator for type checking methods
* @returns The argument code, possibly wrapped with static_cast
*/
const wrapWithCppEnumCast = (
argCode: string,
argExpr: ExpressionContext,
targetParamBaseType: string | undefined,
orchestrator: IOrchestrator,
): string => {
if (!orchestrator.isCppMode() || !targetParamBaseType) {
return argCode;
}
const argType = orchestrator.getExpressionType(argExpr);
if (argType && orchestrator.isCppEnumClass(argType)) {
if (orchestrator.isIntegerType(targetParamBaseType)) {
const cType = CallExprUtils.mapTypeToCType(targetParamBaseType);
return `static_cast<${cType}>(${argCode})`;
}
}
return argCode;
};
/**
* Resolved parameter info from local signature or cross-file lookup
*/
interface IResolvedParam {
param: { baseType: string; isArray?: boolean } | undefined;
isCrossFile: boolean;
}
/**
* Generate argument code for a C/C++ function call.
* Handles automatic address-of (&) for struct arguments passed to pointer params.
*/
const _generateCFunctionArg = (
e: ExpressionContext,
targetParam: IResolvedParam["param"],
input: IGeneratorInput,
orchestrator: IOrchestrator,
): string => {
let argCode = orchestrator.generateExpression(e);
// Issue #322: Check if parameter expects a pointer and argument is a struct
if (!targetParam?.baseType?.endsWith("*")) {
return wrapWithCppEnumCast(argCode, e, targetParam?.baseType, orchestrator);
}
// Try getExpressionType first
let argType = orchestrator.getExpressionType(e);
// Issue #322: If getExpressionType returns null (e.g., for this.member),
// fall back to looking up the generated code in the type registry
if (!argType && !argCode.startsWith("&")) {
const typeInfo = input.typeRegistry.get(argCode);
if (typeInfo) {
argType = typeInfo.baseType;
}
}
// Add & if argument is a struct type (not already a pointer)
const needsAddressOf =
argType &&
!argType.endsWith("*") &&
!argCode.startsWith("&") &&
!targetParam.isArray &&
orchestrator.isStructType(argType);
if (needsAddressOf) {
argCode = `&${argCode}`;
}
return wrapWithCppEnumCast(argCode, e, targetParam?.baseType, orchestrator);
};
/**
* Determine if a C-Next parameter should be passed by value.
*/
const _shouldPassByValue = (
funcExpr: string,
idx: number,
targetParam: IResolvedParam["param"],
isCrossFile: boolean,
orchestrator: IOrchestrator,
): boolean => {
if (!targetParam) return false;
const isFloatParam = orchestrator.isFloatType(targetParam.baseType);
const isEnumParam = orchestrator.getKnownEnums().has(targetParam.baseType);
const isPrimitivePassByValue = orchestrator.isParameterPassByValue(
funcExpr,
idx,
);
const isSmallPrimitive =
isCrossFile && CallExprUtils.isSmallPrimitiveType(targetParam.baseType);
// Issue #551: Unknown types (external enums, typedefs) use pass-by-value
const isUnknownType =
!orchestrator.isStructType(targetParam.baseType) &&
!CallExprUtils.isKnownPrimitiveType(targetParam.baseType) &&
!CallExprUtils.isStringType(targetParam.baseType) &&
!isFloatParam &&
!isEnumParam &&
!isSmallPrimitive;
return (
isFloatParam ||
isEnumParam ||
isPrimitivePassByValue ||
isSmallPrimitive ||
isUnknownType
);
};
/**
* Generate C code for a function call.
*
* @param funcExpr - The function name or expression being called
* @param argCtx - The argument list context (null for empty calls)
* @param input - Generator input (type registry, function signatures, etc.)
* @param _state - Generator state (unused but part of signature)
* @param orchestrator - Orchestrator for callbacks into CodeGenerator
* @returns Generated code and effects
*/
const generateFunctionCall = (
funcExpr: string,
argCtx: ArgumentListContext | null,
input: IGeneratorInput,
_state: IGeneratorState,
orchestrator: IOrchestrator,
): IGeneratorOutput => {
const effects: TGeneratorEffect[] = [];
// Empty function call
if (!argCtx) {
return { code: `${funcExpr}()`, effects };
}
const argExprs = argCtx.expression();
// Check if this is a C-Next function (uses pass-by-reference)
const isCNextFunc = orchestrator.isCNextFunction(funcExpr);
// ADR-051: Handle safe_div() and safe_mod() built-in functions
if (funcExpr === "safe_div" || funcExpr === "safe_mod") {
return generateSafeDivMod(funcExpr, argExprs, input, orchestrator, effects);
}
// Regular function call handling
// ADR-013: Check const-to-non-const before generating arguments
if (isCNextFunc) {
validateConstToNonConst(funcExpr, argExprs, input, orchestrator);
// Issue #268: Track pass-through modifications for auto-const
trackPassThroughModifications(funcExpr, argExprs, orchestrator);
}
// Get function signature once for all arguments
const sig = input.functionSignatures.get(funcExpr);
const args = argExprs
.map((e, idx) => {
// Get parameter type info from local signature or cross-file SymbolTable
const resolved = CallExprUtils.resolveTargetParam(
sig,
idx,
funcExpr,
input.symbolTable,
);
const targetParam = resolved.param;
// C/C++ function: use pass-by-value semantics
if (!isCNextFunc) {
return _generateCFunctionArg(e, targetParam, input, orchestrator);
}
// C-Next function: check if target parameter should be passed by value
if (
_shouldPassByValue(
funcExpr,
idx,
targetParam,
resolved.isCrossFile,
orchestrator,
)
) {
const argCode = orchestrator.generateExpression(e);
return wrapWithCppEnumCast(
argCode,
e,
targetParam?.baseType,
orchestrator,
);
}
// Target parameter is pass-by-reference: use & logic
return orchestrator.generateFunctionArg(e, targetParam?.baseType);
})
.join(", ");
return { code: `${funcExpr}(${args})`, effects };
};
/**
* Generate code for safe_div() or safe_mod() built-in functions (ADR-051).
*
* These functions take 4 arguments:
* - output: Variable to store result (passed by reference)
* - numerator: The dividend
* - divisor: The divisor
* - defaultValue: Value to use if divisor is 0
*/
const generateSafeDivMod = (
funcName: string,
argExprs: ExpressionContext[],
input: IGeneratorInput,
orchestrator: IOrchestrator,
effects: TGeneratorEffect[],
): IGeneratorOutput => {
if (argExprs.length !== 4) {
throw new Error(
`${funcName} requires exactly 4 arguments: output, numerator, divisor, defaultValue`,
);
}
// Get the output parameter (first argument) to determine type
const outputArgId = orchestrator.getSimpleIdentifier(argExprs[0]);
if (!outputArgId) {
throw new Error(
`${funcName} requires a variable as the first argument (output parameter)`,
);
}
// Look up the type of the output parameter
const typeInfo = input.typeRegistry.get(outputArgId);
if (!typeInfo) {
throw new Error(
`Cannot determine type of output parameter '${outputArgId}' for ${funcName}`,
);
}
// Map C-Next type to helper function suffix
const cnxType = typeInfo.baseType;
if (!cnxType) {
throw new Error(
`Output parameter '${outputArgId}' has no C-Next type for ${funcName}`,
);
}
// Generate arguments: &output, numerator, divisor, defaultValue
const outputArg = `&${orchestrator.generateExpression(argExprs[0])}`;
const numeratorArg = orchestrator.generateExpression(argExprs[1]);
const divisorArg = orchestrator.generateExpression(argExprs[2]);
const defaultArg = orchestrator.generateExpression(argExprs[3]);
const helperName = CallExprUtils.generateSafeDivModHelperName(
funcName as "safe_div" | "safe_mod",
cnxType,
);
// Track that this operation is used for helper generation
const opType: "div" | "mod" = funcName === "safe_div" ? "div" : "mod";
effects.push({ type: "safe-div", operation: opType, cnxType });
return {
code: `${helperName}(${outputArg}, ${numeratorArg}, ${divisorArg}, ${defaultArg})`,
effects,
};
};
/**
* Validate const-to-non-const parameter passing (ADR-013).
*
* Throws an error if a const value is passed to a non-const parameter.
*/
const validateConstToNonConst = (
funcName: string,
argExprs: ExpressionContext[],
input: IGeneratorInput,
orchestrator: IOrchestrator,
): void => {
const sig = input.functionSignatures.get(funcName);
if (!sig) return;
for (
let argIdx = 0;
argIdx < argExprs.length && argIdx < sig.parameters.length;
argIdx++
) {
const argId = orchestrator.getSimpleIdentifier(argExprs[argIdx]);
if (argId && orchestrator.isConstValue(argId)) {
const param = sig.parameters[argIdx];
if (!param.isConst) {
throw new Error(
`cannot pass const '${argId}' to non-const parameter '${param.name}' ` +
`of function '${funcName}'`,
);
}
}
}
};
/**
* Issue #268: Track pass-through modifications for auto-const inference.
*
* When a parameter of the current function is passed to another function
* that modifies its corresponding parameter, we must mark our parameter
* as modified too (since it's pass-by-reference).
*
* Example:
* void modifies(u32 val) { val <- 42; }
* void passesThrough(u32 val) { modifies(val); } // val is effectively modified
*
* Note: This only works when the callee is defined before the caller.
* If the callee is defined later, we can't know if it modifies the param,
* and the C compiler will catch any const-mismatch errors.
*/
const trackPassThroughModifications = (
funcName: string,
argExprs: ExpressionContext[],
orchestrator: IOrchestrator,
): void => {
for (let argIdx = 0; argIdx < argExprs.length; argIdx++) {
const argId = orchestrator.getSimpleIdentifier(argExprs[argIdx]);
if (!argId) continue;
// Check if this argument is a parameter of the current function
if (!orchestrator.isCurrentParameter(argId)) continue;
// Check if the callee's parameter at this index is modified
if (orchestrator.isCalleeParameterModified(funcName, argIdx)) {
// The callee modifies this parameter, so our parameter is also modified
orchestrator.markParameterModified(argId);
}
}
};
export default generateFunctionCall;
|