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 | 30x 9x 21x 4x 17x 17x 2x 15x 15x 2x 2x 2x 2x 13x 6x 1x 5x 5x 1x 1x 4x 4x 4x 1x 3x 3x 3x 3x 3x 3x 3x 9x 3x 6x 6x 3x 3x 12x 12x 3x 9x 1x 8x 11x 8x 8x 9x 9x 9x 3x 6x 30x 22x 8x 8x 7x 1x | /**
* ArgumentGenerator - Generates function arguments with proper ADR-006 semantics
*
* Issue #794: Extracted from CodeGenerator to reduce file size.
* Uses CodeGenState for state access and callbacks for CodeGenerator methods.
*
* Handles argument generation patterns:
* - Local variables get & (address-of) in C mode
* - Member access (cursor.x) gets & (address-of)
* - Array access (arr[i]) gets & (address-of)
* - Parameters are passed as-is (already pointers)
* - Arrays are passed as-is (naturally decay to pointers)
* - Literals use compound literals for pointer params: &(type){value}
* - Complex expressions are passed as-is
*/
import * as Parser from "../../../logic/parser/grammar/CNextParser.js";
import CodeGenState from "../../../state/CodeGenState.js";
import CppModeHelper from "./CppModeHelper.js";
import TYPE_MAP from "../types/TYPE_MAP.js";
import IArgumentGeneratorCallbacks from "./types/IArgumentGeneratorCallbacks.js";
/**
* Generates function arguments with proper pass-by-reference semantics.
*/
class ArgumentGenerator {
/**
* Handle simple identifier argument (parameter, local array, scope member, or variable).
* This is a pure function that only reads from CodeGenState.
*/
static handleIdentifierArg(id: string): string {
// Parameters are already pointers
if (CodeGenState.currentParameters.get(id)) {
return id;
}
// Local arrays decay to pointers
if (CodeGenState.localArrays.has(id)) {
return id;
}
// Global arrays also decay to pointers (check typeRegistry)
// But NOT strings - strings need & (they're char arrays but passed by reference)
const typeInfo = CodeGenState.getVariableTypeInfo(id);
if (typeInfo?.isArray && !typeInfo.isString) {
return id;
}
// Issue #895 Bug B: Inferred pointers are already pointers, don't add &
Iif (typeInfo?.isPointer) {
return id;
}
// Scope member - may need prefixing
if (CodeGenState.currentScope) {
const members = CodeGenState.getScopeMembers(CodeGenState.currentScope);
Eif (members?.has(id)) {
const scopedName = `${CodeGenState.currentScope}_${id}`;
return CppModeHelper.maybeAddressOf(scopedName);
}
}
// Local variable - add & (except in C++ mode)
return CppModeHelper.maybeAddressOf(id);
}
/**
* Handle rvalue argument (literals or complex expressions).
* Issue #872: Sets expectedType for MISRA 7.2 U suffix on unsigned literals.
*/
static handleRvalueArg(
ctx: Parser.ExpressionContext,
targetParamBaseType: string | undefined,
callbacks: IArgumentGeneratorCallbacks,
): string {
// Issue #872: Early return when no target type - no state management needed
if (!targetParamBaseType) {
return callbacks.generateExpression(ctx);
}
const cType = TYPE_MAP[targetParamBaseType];
if (!cType || cType === "void") {
// Issue #872: Suppress bare enum resolution in function args (requires ADR to change)
return CodeGenState.withExpectedType(
targetParamBaseType,
() => callbacks.generateExpression(ctx),
true, // suppressEnumResolution
);
}
// Issue #872: Suppress bare enum resolution in function args (requires ADR to change)
const value = CodeGenState.withExpectedType(
targetParamBaseType,
() => callbacks.generateExpression(ctx),
true, // suppressEnumResolution
);
// C++ mode: rvalues can bind to const T&
if (CodeGenState.cppMode) {
return value;
}
// C mode: Use compound literal syntax
return `&(${cType}){${value}}`;
}
/**
* Create temp variable for C++ member conversion.
*/
static createCppMemberConversionTemp(
ctx: Parser.ExpressionContext,
targetParamBaseType: string,
callbacks: IArgumentGeneratorCallbacks,
): string {
const cType = TYPE_MAP[targetParamBaseType] || "uint8_t";
const value = callbacks.generateExpression(ctx);
const tempName = `_cnx_tmp_${CodeGenState.tempVarCounter++}`;
const castExpr = CppModeHelper.cast(cType, value);
CodeGenState.pendingTempDeclarations.push(
`${cType} ${tempName} = ${castExpr};`,
);
return CppModeHelper.maybeAddressOf(tempName);
}
/**
* Maybe cast string subscript access for integer pointer parameters.
*/
static maybeCastStringSubscript(
ctx: Parser.ExpressionContext,
expr: string,
targetParamBaseType: string | undefined,
callbacks: IArgumentGeneratorCallbacks,
): string {
if (!targetParamBaseType || !callbacks.isStringSubscriptAccess(ctx)) {
return expr;
}
const cType = TYPE_MAP[targetParamBaseType];
if (cType && !["float", "double", "bool", "void"].includes(cType)) {
return CppModeHelper.reinterpretCast(`${cType}*`, expr);
}
return expr;
}
/**
* Handle member access argument - may need special handling for arrays or C++ conversions.
* Returns null if default lvalue handling should be used.
*/
static handleMemberAccessArg(
ctx: Parser.ExpressionContext,
targetParamBaseType: string | undefined,
callbacks: IArgumentGeneratorCallbacks,
): string | null {
const arrayStatus = callbacks.getMemberAccessArrayStatus(ctx);
// Array member - no address-of needed
if (arrayStatus === "array") {
return callbacks.generateExpression(ctx);
}
// C++ mode may need temp variable for type conversion
if (
arrayStatus === "not-array" &&
targetParamBaseType &&
callbacks.needsCppMemberConversion(ctx, targetParamBaseType)
) {
return ArgumentGenerator.createCppMemberConversionTemp(
ctx,
targetParamBaseType,
callbacks,
);
}
return null; // Fall through to default lvalue handling
}
/**
* Handle lvalue argument (member access or array access).
*/
static handleLvalueArg(
ctx: Parser.ExpressionContext,
lvalueType: "member" | "array",
targetParamBaseType: string | undefined,
callbacks: IArgumentGeneratorCallbacks,
): string {
// Member access to array field - arrays decay to pointers
if (lvalueType === "member") {
const memberResult = ArgumentGenerator.handleMemberAccessArg(
ctx,
targetParamBaseType,
callbacks,
);
if (memberResult) return memberResult;
}
// Generate expression with address-of
const generatedExpr = callbacks.generateExpression(ctx);
const expr = CppModeHelper.maybeAddressOf(generatedExpr);
// String subscript access may need cast
if (lvalueType === "array") {
return ArgumentGenerator.maybeCastStringSubscript(
ctx,
expr,
targetParamBaseType,
callbacks,
);
}
return expr;
}
/**
* Main entry point: Generate a function argument with proper ADR-006 semantics.
*
* @param ctx - The expression context
* @param simpleId - The simple identifier if known (optimization to avoid re-parsing)
* @param targetParamBaseType - The target parameter's base type
* @param callbacks - Callbacks to CodeGenerator methods
*/
static generateArg(
ctx: Parser.ExpressionContext,
simpleId: string | null,
targetParamBaseType: string | undefined,
callbacks: IArgumentGeneratorCallbacks,
): string {
// Handle simple identifiers
if (simpleId) {
return ArgumentGenerator.handleIdentifierArg(simpleId);
}
// Check if expression is an lvalue
const lvalueType = callbacks.getLvalueType(ctx);
if (lvalueType) {
return ArgumentGenerator.handleLvalueArg(
ctx,
lvalueType,
targetParamBaseType,
callbacks,
);
}
// Handle rvalue (literals or complex expressions)
return ArgumentGenerator.handleRvalueArg(
ctx,
targetParamBaseType,
callbacks,
);
}
}
export default ArgumentGenerator;
|