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 | 68x 20x 48x 48x 9x 39x 39x 6x 1x 5x 5x 1x 1x 4x 4x 4x 1x 3x 3x 3x 3x 3x 3x 3x 9x 3x 6x 6x 3x 3x 17x 17x 5x 12x 1x 11x 16x 13x 13x 12x 12x 12x 3x 9x 71x 58x 13x 13x 12x 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 CppModeHelper from "./CppModeHelper";
import TYPE_MAP from "../types/TYPE_MAP";
import IArgumentGeneratorCallbacks from "./types/IArgumentGeneratorCallbacks";
import type TranspileState from "../../../TranspileState";
import type TTypeInfo from "../../../../types/TTypeInfo";
/**
* Generates function arguments with proper pass-by-reference semantics.
*/
class ArgumentGenerator {
/**
* Handle simple identifier argument (parameter, array, pointer, or variable).
* This is a pure function that only reads from state.
*
* @param id the name as written, which is how parameters are keyed
* @param emitted the C name the one binder gives it (#1760 review):
* a shadowing local's ADR-057 name, or a scope member's qualified name.
* This used to be decided here from `getScopeMembers().has(id)` or the
* bare `id`, so a local shadowing a global or a member was passed as the
* global or the member, and a scope member array as its bare name.
*/
static handleIdentifierArg(
id: string,
emitted: string,
declared: TTypeInfo | undefined,
state: TranspileState,
): string {
// Parameters are already pointers
if (state.currentParameters.get(id)) {
return id;
}
// Arrays decay to pointers, local or not, strings included (#1668: the
// declaration's answer, which the `localArrays` set only duplicated): a
// `char[N]` decays to `char*`
// exactly like any other array, and a `string<N>` parameter is generated as
// `char*`. Taking its address instead yields `char (*)[N]`, an incompatible
// pointer type -- the defect `e3dff5f4` fixed by deleting the `isString`
// exception this comment used to argue for.
const typeInfo = declared;
if (typeInfo?.isArray) {
return emitted;
}
// Issue #895 Bug B: Inferred pointers are already pointers, don't add &
Iif (typeInfo?.isPointer) {
return emitted;
}
// A variable - add & (except in C++ mode)
return CppModeHelper.maybeAddressOf(emitted, state);
}
/**
* Handle rvalue argument (literals or complex expressions).
* Issue #872: Sets expectedType for MISRA 7.2 U suffix on unsigned literals.
*/
static handleRvalueArg(
targetParamBaseType: string | undefined,
callbacks: IArgumentGeneratorCallbacks,
state: TranspileState,
): string {
// Issue #872: Early return when no target type - no state management needed
if (!targetParamBaseType) {
return callbacks.generateExpression();
}
const cType = TYPE_MAP[targetParamBaseType];
if (!cType || cType === "void") {
// Issue #872: Suppress bare enum resolution in function args (requires ADR to change)
return state.withExpectedType(
targetParamBaseType,
() => callbacks.generateExpression(),
true, // suppressEnumResolution
);
}
// Issue #872: Suppress bare enum resolution in function args (requires ADR to change)
const value = state.withExpectedType(
targetParamBaseType,
() => callbacks.generateExpression(),
true, // suppressEnumResolution
);
// C++ mode: rvalues can bind to const T&
if (state.cppMode) {
return value;
}
// C mode: Use compound literal syntax
return `&(${cType}){${value}}`;
}
/**
* Create temp variable for C++ member conversion.
*/
static createCppMemberConversionTemp(
targetParamBaseType: string,
callbacks: IArgumentGeneratorCallbacks,
state: TranspileState,
): string {
const cType = TYPE_MAP[targetParamBaseType] || "uint8_t";
const value = callbacks.generateExpression();
// Issue #1131: one temporary namer for every family. This site previously
// incremented the shared counter itself and spelled the name a third way
// (`_cnx_tmp_<N>` alongside `_tmp<N>`), so the two families agreed only by
// coincidence of drawing from the same counter.
const tempName = state.getNextTempVarName();
const castExpr = CppModeHelper.cast(cType, value, state);
state.pendingTempDeclarations.push(`${cType} ${tempName} = ${castExpr};`);
return CppModeHelper.maybeAddressOf(tempName, state);
}
/**
* Maybe cast string subscript access for integer pointer parameters.
*/
static maybeCastStringSubscript(
expr: string,
targetParamBaseType: string | undefined,
callbacks: IArgumentGeneratorCallbacks,
state: TranspileState,
): string {
if (!targetParamBaseType || !callbacks.isStringSubscriptAccess()) {
return expr;
}
const cType = TYPE_MAP[targetParamBaseType];
if (cType && !["float", "double", "bool", "void"].includes(cType)) {
return CppModeHelper.reinterpretCast(`${cType}*`, expr, state);
}
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(
targetParamBaseType: string | undefined,
callbacks: IArgumentGeneratorCallbacks,
state: TranspileState,
): string | null {
const arrayStatus = callbacks.getMemberAccessArrayStatus();
// Array member - no address-of needed
if (arrayStatus === "array") {
return callbacks.generateExpression();
}
// C++ mode may need temp variable for type conversion
if (
arrayStatus === "not-array" &&
targetParamBaseType &&
callbacks.isCppMemberConversionRequired(targetParamBaseType)
) {
return ArgumentGenerator.createCppMemberConversionTemp(
targetParamBaseType,
callbacks,
state,
);
}
return null; // Fall through to default lvalue handling
}
/**
* Handle lvalue argument (member access or array access).
*/
static handleLvalueArg(
lvalueType: "member" | "array",
targetParamBaseType: string | undefined,
callbacks: IArgumentGeneratorCallbacks,
state: TranspileState,
): string {
// Member access to array field - arrays decay to pointers
if (lvalueType === "member") {
const memberResult = ArgumentGenerator.handleMemberAccessArg(
targetParamBaseType,
callbacks,
state,
);
if (memberResult) return memberResult;
}
// Generate expression with address-of
const generatedExpr = callbacks.generateExpression();
const expr = CppModeHelper.maybeAddressOf(generatedExpr, state);
// String subscript access may need cast
if (lvalueType === "array") {
return ArgumentGenerator.maybeCastStringSubscript(
expr,
targetParamBaseType,
callbacks,
state,
);
}
return expr;
}
/**
* Main entry point: Generate a function argument with proper ADR-006 semantics.
*
* @param simpleId - The simple identifier as written, and the C name it binds to
* @param targetParamBaseType - The target parameter's base type
* @param callbacks - Callbacks to CodeGenerator methods
*/
static generateArg(
simpleId: { readonly id: string; readonly emitted: string } | null,
declared: TTypeInfo | undefined,
targetParamBaseType: string | undefined,
callbacks: IArgumentGeneratorCallbacks,
state: TranspileState,
): string {
// Handle simple identifiers
if (simpleId) {
return ArgumentGenerator.handleIdentifierArg(
simpleId.id,
simpleId.emitted,
declared,
state,
);
}
// Check if expression is an lvalue
const lvalueType = callbacks.getLvalueType();
if (lvalueType) {
return ArgumentGenerator.handleLvalueArg(
lvalueType,
targetParamBaseType,
callbacks,
state,
);
}
// Handle rvalue (literals or complex expressions)
return ArgumentGenerator.handleRvalueArg(
targetParamBaseType,
callbacks,
state,
);
}
}
export default ArgumentGenerator;
|