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 | 154x 154x 154x 10x 144x 59x 59x 14x 130x 130x 48x 82x 82x 71x 11x 11x 11x 11x 1x 10x 130x 71x 59x 59x 53x 53x 51x 51x 49x 49x 31x 18x 59x 53x 6x 6x 6x 53x 49x 4x 4x 51x 44x 7x 7x 7x 7x 2x 5x 18x 7x 11x 11x 11x 11x 154x 154x 142x 12x 12x 12x 12x 6x 6x 5x 2x 3x 2x 1x 1x 1x 1x 1x 12x 12x 12x 1x 11x 10x 1x | /**
* EnumTypeResolver - Handles enum type inference from expressions
*
* Extracted from CodeGenerator to reduce complexity.
* Uses CodeGenState for all state access.
*
* ADR-017: Extract enum type from expressions for type-safe comparisons.
* Handles patterns:
* - Variable of enum type: `currentState` -> 'State'
* - Enum member access: `State.IDLE` -> 'State'
* - Scoped enum member: `Motor.State.IDLE` -> 'Motor_State'
* - ADR-016: this.State.IDLE -> 'CurrentScope_State'
* - ADR-016: this.variable -> enum type if variable is of enum type
* - Function calls returning enum types
*/
import * as Parser from "../../../logic/parser/grammar/CNextParser";
import CodeGenState from "../../../state/CodeGenState";
import TypeResolver from "../TypeResolver";
import ExpressionUnwrapper from "../../../../utils/ExpressionUnwrapper";
import QualifiedNameGenerator from "../utils/QualifiedNameGenerator";
/**
* Resolves enum types from expressions.
* All methods are static - uses CodeGenState for state access.
*/
export default class EnumTypeResolver {
/**
* Extract enum type from an expression.
* Returns the enum type name if the expression is an enum value, null otherwise.
*/
static resolve(
ctx: Parser.ExpressionContext | Parser.RelationalExpressionContext,
): string | null {
const text = ctx.getText();
// Check if it's a function call returning an enum
const enumReturnType = this.getFunctionCallEnumType(text);
if (enumReturnType) {
return enumReturnType;
}
// Check if it's a simple identifier that's an enum variable
if (/^[a-zA-Z_]\w*$/.exec(text)) {
const typeInfo = CodeGenState.getVariableTypeInfo(text);
if (typeInfo?.isEnum && typeInfo.enumTypeName) {
return typeInfo.enumTypeName;
}
}
// Check member access patterns: EnumType.MEMBER, Scope.EnumType.MEMBER, etc.
const memberResult = this.getEnumTypeFromMemberAccess(text.split("."));
if (memberResult) {
return memberResult;
}
// Fallback: use TypeResolver to resolve the full expression type through
// struct member chains (e.g. global.config.inputs[0].assignedValue -> EValueId)
return this.resolveViaTypeResolver(ctx);
}
/**
* Fallback resolution via TypeResolver for complex expressions.
* Handles struct member chains like global.struct.field that resolve to enum types.
*/
private static resolveViaTypeResolver(
ctx: Parser.ExpressionContext | Parser.RelationalExpressionContext,
): string | null {
// ExpressionContext has getPostfixExpression, RelationalExpressionContext does not
if (!("ternaryExpression" in ctx)) {
return null;
}
const postfix = ExpressionUnwrapper.getPostfixExpression(ctx);
Iif (!postfix) {
return null;
}
const resolvedType = TypeResolver.getPostfixExpressionType(postfix);
if (resolvedType && CodeGenState.isKnownEnum(resolvedType)) {
return resolvedType;
}
return null;
}
/**
* Check if parts represent an enum member access and return the enum type.
*/
private static getEnumTypeFromMemberAccess(parts: string[]): string | null {
if (parts.length < 2) {
return null;
}
// ADR-016: Check this.State.IDLE pattern
const thisEnumType = this.getEnumTypeFromThisEnum(parts);
if (thisEnumType) return thisEnumType;
// Issue #478: Check global.Enum.Member pattern
const globalEnumType = this.getEnumTypeFromGlobalEnum(parts);
if (globalEnumType) return globalEnumType;
// ADR-016: Check this.variable pattern
const thisVarType = this.getEnumTypeFromThisVariable(parts);
if (thisVarType) return thisVarType;
// Check simple enum: State.IDLE
const possibleEnum = parts[0];
if (CodeGenState.isKnownEnum(possibleEnum)) {
return possibleEnum;
}
// Check scoped enum: Motor.State.IDLE -> Motor_State
return this.getEnumTypeFromScopedEnum(parts);
}
/**
* ADR-016: Check this.State.IDLE pattern (this.Enum.Member inside scope)
*/
private static getEnumTypeFromThisEnum(parts: string[]): string | null {
if (parts[0] !== "this" || !CodeGenState.currentScope || parts.length < 3) {
return null;
}
const enumName = parts[1];
const scopedEnumName = `${CodeGenState.currentScope}_${enumName}`;
return CodeGenState.isKnownEnum(scopedEnumName) ? scopedEnumName : null;
}
/**
* Issue #478: Check global.Enum.Member pattern (global.ECategory.CAT_A)
*/
private static getEnumTypeFromGlobalEnum(parts: string[]): string | null {
if (parts[0] !== "global" || parts.length < 3) {
return null;
}
const enumName = parts[1];
return CodeGenState.isKnownEnum(enumName) ? enumName : null;
}
/**
* ADR-016: Check this.variable pattern (this.varName where varName is enum type)
*/
private static getEnumTypeFromThisVariable(parts: string[]): string | null {
if (
parts[0] !== "this" ||
!CodeGenState.currentScope ||
parts.length !== 2
) {
return null;
}
const varName = parts[1];
const scopedVarName = `${CodeGenState.currentScope}_${varName}`;
const typeInfo = CodeGenState.getVariableTypeInfo(scopedVarName);
if (typeInfo?.isEnum && typeInfo.enumTypeName) {
return typeInfo.enumTypeName;
}
return null;
}
/**
* Check scoped enum: Motor.State.IDLE -> Motor_State
*/
private static getEnumTypeFromScopedEnum(parts: string[]): string | null {
if (parts.length < 3) {
return null;
}
const scopeName = parts[0];
const enumName = parts[1];
const scopedEnumName = QualifiedNameGenerator.forMember(
scopeName,
enumName,
);
return CodeGenState.isKnownEnum(scopedEnumName) ? scopedEnumName : null;
}
/**
* Check if an expression is a function call returning an enum type.
* Handles patterns:
* - func() or func(args) - global function
* - Scope.method() or Scope.method(args) - scope method from outside
* - this.method() or this.method(args) - scope method from inside
* - global.func() or global.func(args) - global function from inside scope
* - global.Scope.method() or global.Scope.method(args) - scope method from inside another scope
*/
private static getFunctionCallEnumType(text: string): string | null {
// Check if this looks like a function call (contains parentheses)
const parenIndex = text.indexOf("(");
if (parenIndex === -1) {
return null;
}
// Extract the function reference (everything before the opening paren)
const funcRef = text.substring(0, parenIndex);
const parts = funcRef.split(".");
let fullFuncName: string | null = null;
if (parts.length === 1) {
// Simple function call: func()
fullFuncName = parts[0];
} else if (parts.length === 2) {
if (parts[0] === "this" && CodeGenState.currentScope) {
// this.method() -> Scope_method
fullFuncName = `${CodeGenState.currentScope}_${parts[1]}`;
} else if (parts[0] === "global") {
// global.func() -> func
fullFuncName = parts[1];
E} else if (CodeGenState.isKnownScope(parts[0])) {
// Scope.method() -> Scope_method
fullFuncName = `${parts[0]}_${parts[1]}`;
}
E} else if (parts.length === 3) {
Eif (parts[0] === "global" && CodeGenState.isKnownScope(parts[1])) {
// global.Scope.method() -> Scope_method
fullFuncName = `${parts[1]}_${parts[2]}`;
}
}
Iif (!fullFuncName) {
return null;
}
// Look up the function's return type
const returnType = CodeGenState.getFunctionReturnType(fullFuncName);
if (!returnType) {
return null;
}
// Check if the return type is an enum
if (CodeGenState.isKnownEnum(returnType)) {
return returnType;
}
return null;
}
}
|