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 | 854x 21x 21x 21x 2x 19x 19x 19x 5x 14x 30x 14x 14x 4x 10x 6x 4x 1x 3x 3x 3x 14x 14x 3x 11x 14x 13x 13x 9x 9x 1x 4x 10x 9x 9x 1x 8x 8x 8x 8x 8x 8x 8x 9x 9x 9x 5x 4x 4x 4x 2x 2x 4x 4x 3x 3x 5x 2x 3x 3x 3x 3x | /**
* MemberChainAnalyzer - Analyzes member access chains for bit access patterns
*
* Issue #644: Extracted from CodeGenerator to reduce file size.
* Refactored to delegate to buildMemberAccessChain to eliminate code duplication.
*
* Used to detect bit access at the end of member chains, e.g.:
* - grid[2][3].flags[0] - detects that [0] is bit access on flags
* - point.x[3, 4] - detects bit range access on integer field
*/
import * as Parser from "../../../logic/parser/grammar/CNextParser.js";
import TTypeInfo from "../types/TTypeInfo.js";
/**
* Result of analyzing a member chain for bit access.
*/
interface IBitAccessAnalysisResult {
/** True if the last subscript is bit access on an integer */
isBitAccess: boolean;
/** The base target expression (without bit index) */
baseTarget?: string;
/** The bit index expression */
bitIndex?: string;
/** The base type of the target */
baseType?: string;
}
/**
* Dependencies required for member chain analysis.
*/
interface IMemberChainAnalyzerDeps {
/** Type registry for looking up variable types */
typeRegistry: ReadonlyMap<string, TTypeInfo>;
/** Struct field types by struct name */
structFields: ReadonlyMap<string, ReadonlyMap<string, string>>;
/** Struct field array flags by struct name */
structFieldArrays: ReadonlyMap<string, ReadonlySet<string>>;
/** Function to check if a type name is a known struct */
isKnownStruct: (name: string) => boolean;
/** Function to generate expression code */
generateExpression: (ctx: Parser.ExpressionContext) => string;
}
/** Mutable state for tracking types through a member chain. */
interface IChainState {
currentType: string;
currentStructType: string | undefined;
isCurrentArray: boolean;
arrayDimsRemaining: number;
}
/**
* Analyzes member access chains to detect bit access patterns.
*
* Delegates to buildMemberAccessChain with type tracking and bit access
* detection callbacks to determine if the final subscript is bit access.
*/
class MemberChainAnalyzer {
private readonly deps: IMemberChainAnalyzerDeps;
constructor(deps: IMemberChainAnalyzerDeps) {
this.deps = deps;
}
/**
* Analyze a member chain target to detect bit access at the end.
*
* For patterns like grid[2][3].flags[0], detects that [0] is bit access.
* Uses direct postfixTargetOp analysis with type tracking.
*
* @param targetCtx - The assignment target context to analyze
* @returns Analysis result with bit access information
*/
analyze(targetCtx: Parser.AssignmentTargetContext): IBitAccessAnalysisResult {
const baseId = targetCtx.IDENTIFIER()?.getText();
const postfixOps = targetCtx.postfixTargetOp();
if (!baseId || postfixOps.length === 0) {
return { isBitAccess: false };
}
// Check if the last postfix op is a single-expression subscript (potential bit access)
const lastOp = postfixOps.at(-1)!;
const lastExprs = lastOp.expression();
if (lastExprs.length !== 1 || lastOp.IDENTIFIER()) {
// Last op is member access or multi-expression subscript, not bit access
return { isBitAccess: false };
}
// Count total subscript operations
const subscriptCount = postfixOps.filter(
(op) => !op.IDENTIFIER() && op.expression().length > 0,
).length;
// Walk through the chain to find the type and array status before the last subscript
const targetInfo = this.resolveTargetTypeAndArrayStatus(
baseId,
postfixOps.slice(0, -1),
subscriptCount - 1, // subscripts before the last one
);
if (!targetInfo) {
return { isBitAccess: false };
}
// If the target is still an array, the last subscript is array access, not bit access
if (targetInfo.isArray) {
return { isBitAccess: false };
}
// Check if the type is an integer (bit access only works on integers)
if (!this.isIntegerType(targetInfo.type)) {
return { isBitAccess: false };
}
// Build the base target expression (everything except the last subscript)
const baseTarget = this.buildBaseTarget(baseId, postfixOps.slice(0, -1));
const bitIndex = this.deps.generateExpression(lastExprs[0]);
return {
isBitAccess: true,
baseTarget,
bitIndex,
baseType: targetInfo.type,
};
}
/**
* Resolve the type and array status of the target by walking through postfix operations.
* Returns the type and whether it's still an array before the last subscript.
*/
private resolveTargetTypeAndArrayStatus(
baseId: string,
ops: Parser.PostfixTargetOpContext[],
_subscriptsSoFar: number,
): { type: string; isArray: boolean } | undefined {
const baseTypeInfo = this.deps.typeRegistry.get(baseId);
if (!baseTypeInfo) {
return undefined;
}
const state: IChainState = {
currentType: baseTypeInfo.baseType,
currentStructType: this.deps.isKnownStruct(baseTypeInfo.baseType)
? baseTypeInfo.baseType
: undefined,
isCurrentArray: baseTypeInfo.isArray,
arrayDimsRemaining: baseTypeInfo.arrayDimensions?.length ?? 0,
};
for (let i = 0; i < ops.length; i++) {
const op = ops[i];
if (op.IDENTIFIER()) {
const result = this.processMemberOp(op, ops, i, state);
if (!result) {
return undefined;
}
} else {
this.processSubscriptOp(state);
}
}
return { type: state.currentType, isArray: state.isCurrentArray };
}
/**
* Process a member access operation (.fieldName) and update chain state.
* Returns false if the access is invalid.
*/
private processMemberOp(
op: Parser.PostfixTargetOpContext,
ops: Parser.PostfixTargetOpContext[],
opIndex: number,
state: IChainState,
): boolean {
const fieldName = op.IDENTIFIER()!.getText();
if (!state.currentStructType) {
return false;
}
const structFields = this.deps.structFields.get(state.currentStructType);
Iif (!structFields) {
return false;
}
const fieldType = structFields.get(fieldName);
Iif (!fieldType) {
return false;
}
state.currentType = fieldType;
// Check if this field is an array
const arrayFields = this.deps.structFieldArrays.get(
state.currentStructType,
);
state.isCurrentArray = arrayFields?.has(fieldName) ?? false;
// If the field type is a struct, update currentStructType
state.currentStructType = this.deps.isKnownStruct(state.currentType)
? state.currentType
: undefined;
// Calculate array dimensions remaining based on remaining subscripts
state.arrayDimsRemaining = state.isCurrentArray
? this.countRemainingSubscripts(ops, opIndex) + 1
: 0;
return true;
}
/**
* Count remaining subscript operations after the given index.
*/
private countRemainingSubscripts(
ops: Parser.PostfixTargetOpContext[],
afterIndex: number,
): number {
return ops
.slice(afterIndex + 1)
.filter((o) => !o.IDENTIFIER() && o.expression().length > 0).length;
}
/**
* Process a subscript operation ([expr]) and update chain state.
*/
private processSubscriptOp(state: IChainState): void {
Iif (!state.isCurrentArray || state.arrayDimsRemaining <= 0) {
return;
}
state.arrayDimsRemaining--;
if (state.arrayDimsRemaining === 0) {
state.isCurrentArray = false;
state.currentStructType = this.deps.isKnownStruct(state.currentType)
? state.currentType
: undefined;
}
}
/**
* Check if a type is an integer type.
*/
private isIntegerType(typeName: string): boolean {
const intTypes = new Set([
"u8",
"u16",
"u32",
"u64",
"i8",
"i16",
"i32",
"i64",
]);
return intTypes.has(typeName);
}
/**
* Build the target expression string from base identifier and postfix operations.
*/
private buildBaseTarget(
baseId: string,
ops: Parser.PostfixTargetOpContext[],
): string {
let result = baseId;
for (const op of ops) {
if (op.IDENTIFIER()) {
result += "." + op.IDENTIFIER()!.getText();
} else {
const exprs = op.expression();
if (exprs.length === 1) {
result += "[" + this.deps.generateExpression(exprs[0]) + "]";
E} else if (exprs.length === 2) {
result +=
"[" +
this.deps.generateExpression(exprs[0]) +
", " +
this.deps.generateExpression(exprs[1]) +
"]";
}
}
}
return result;
}
}
export default MemberChainAnalyzer;
|