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 | 329x 323x 6x 6x 6x 6x 6x 6x 3x 3x 3x 3x 2x 3x 3x 26x 26x 26x 21x 5x 5x 18x 20x 20x 20x 20x 20x 203x 203x 203x 203x 203x 329x 329x 329x 203x 329x 329x 329x 329x 329x 329x 329x 329x 329x 329x 6x 6x 6x 329x 3x 3x 3x 326x 16x 16x 329x | /**
* StructCollector - Extracts struct type declarations from parse trees.
* Handles fields with types, arrays, and const modifiers.
*
* Produces TType-based IStructSymbol with proper IScopeSymbol references.
*/
import * as Parser from "../../../parser/grammar/CNextParser";
import ESourceLanguage from "../../../../../utils/types/ESourceLanguage";
import IStructSymbol from "../../../../types/symbols/IStructSymbol";
import IFieldInfo from "../../../../types/symbols/IFieldInfo";
import IScopeSymbol from "../../../../types/symbols/IScopeSymbol";
import TypeResolver from "../../../../../utils/TypeResolver";
import TypeUtils from "../utils/TypeUtils";
import LiteralUtils from "../../../../../utils/LiteralUtils";
/**
* Result of processing an arrayType syntax context.
*/
interface IArrayTypeResult {
isArray: boolean;
dimension: number | undefined;
}
/**
* Process arrayType syntax (e.g., Item[3] items) and return array info.
*/
function processArrayTypeSyntax(
arrayTypeCtx: Parser.ArrayTypeContext | null | undefined,
constValues?: Map<string, number>,
): IArrayTypeResult {
if (!arrayTypeCtx) {
return { isArray: false, dimension: undefined };
}
// Get the first dimension (for backwards compatibility with single-dimension code)
const dims = arrayTypeCtx.arrayTypeDimension();
Iif (dims.length === 0) {
return { isArray: true, dimension: undefined };
}
const sizeExpr = dims[0].expression();
Iif (!sizeExpr) {
return { isArray: true, dimension: undefined };
}
const resolved = tryResolveExpressionDimension(sizeExpr, constValues);
return { isArray: true, dimension: resolved };
}
/**
* Process string type fields and update dimensions array.
*/
function processStringField(
stringCtx: Parser.StringTypeContext,
arrayDims: Parser.ArrayDimensionContext[],
dimensions: (number | string)[],
constValues?: Map<string, number>,
): boolean {
const intLiteral = stringCtx.INTEGER_LITERAL();
Iif (!intLiteral) {
return false;
}
const capacity = Number.parseInt(intLiteral.getText(), 10);
// If there are array dimensions, they come BEFORE string capacity
if (arrayDims.length > 0) {
parseArrayDimensions(arrayDims, dimensions, constValues);
}
// String capacity becomes final dimension (+1 for null terminator)
dimensions.push(capacity + 1);
return true;
}
/**
* Try to resolve a single expression as a numeric dimension.
* Handles integer literals and const references.
*/
function tryResolveExpressionDimension(
sizeExpr: Parser.ExpressionContext,
constValues?: Map<string, number>,
): number | undefined {
const dimText = sizeExpr.getText();
const literalSize = LiteralUtils.parseIntegerLiteral(dimText);
if (literalSize !== undefined) {
return literalSize;
}
Eif (constValues?.has(dimText)) {
return constValues.get(dimText);
}
return undefined;
}
/**
* Parse array dimension expressions and append resolved sizes to dimensions array.
*/
function parseArrayDimensions(
arrayDims: Parser.ArrayDimensionContext[],
dimensions: (number | string)[],
constValues?: Map<string, number>,
): void {
for (const dim of arrayDims) {
const sizeExpr = dim.expression();
Eif (sizeExpr) {
const resolved = tryResolveExpressionDimension(sizeExpr, constValues);
Eif (resolved !== undefined) {
dimensions.push(resolved);
}
}
}
}
class StructCollector {
/**
* Collect a struct declaration and return an IStructSymbol.
*
* @param ctx The struct declaration context
* @param sourceFile Source file path
* @param scope The scope this struct belongs to (IScopeSymbol)
* @param constValues Map of constant names to their numeric values (for resolving array dimensions)
* @returns The struct symbol with TType-based types and scope reference
*/
static collect(
ctx: Parser.StructDeclarationContext,
sourceFile: string,
scope: IScopeSymbol,
constValues?: Map<string, number>,
): IStructSymbol {
const name = ctx.IDENTIFIER().getText();
const line = ctx.start?.line ?? 0;
const scopeName = scope.name === "" ? undefined : scope.name;
const fields = new Map<string, IFieldInfo>();
for (const member of ctx.structMember()) {
const fieldName = member.IDENTIFIER().getText();
const fieldInfo = StructCollector.collectField(
member,
fieldName,
scopeName,
constValues,
);
fields.set(fieldName, fieldInfo);
}
return {
kind: "struct",
name,
scope,
sourceFile,
sourceLine: line,
sourceLanguage: ESourceLanguage.CNext,
isExported: true,
fields,
};
}
/**
* Collect a single struct field and return its IFieldInfo.
* Now includes name and TType-based type.
*/
private static collectField(
member: Parser.StructMemberContext,
fieldName: string,
scopeName?: string,
constValues?: Map<string, number>,
): IFieldInfo {
const typeCtx = member.type();
const fieldTypeStr = TypeUtils.getTypeName(typeCtx, scopeName);
const fieldType = TypeResolver.resolve(fieldTypeStr);
// Note: C-Next struct members don't have const modifier in grammar
const isConst = false;
// C-Next struct members don't have atomic modifier
const isAtomic = false;
const arrayDims = member.arrayDimension();
const dimensions: (number | string)[] = [];
let isArray = false;
// Check for C-Next style arrayType syntax: Item[3] items -> typeCtx.arrayType()
const arrayTypeResult = processArrayTypeSyntax(
typeCtx.arrayType(),
constValues,
);
if (arrayTypeResult.isArray) {
isArray = true;
Eif (arrayTypeResult.dimension !== undefined) {
dimensions.push(arrayTypeResult.dimension);
}
// Note: non-literal, non-const expressions (like global.EnumName.COUNT)
// won't be resolvable at symbol collection time - dimensions stays empty
// but isArray is still true so the field is tracked as an array
}
// Handle string types specially
if (typeCtx.stringType()) {
const stringHandled = processStringField(
typeCtx.stringType()!,
arrayDims,
dimensions,
constValues,
);
Eif (stringHandled) {
isArray = true;
}
} else if (arrayDims.length > 0) {
// Non-string array
isArray = true;
parseArrayDimensions(arrayDims, dimensions, constValues);
}
return {
name: fieldName,
type: fieldType,
isConst,
isAtomic,
isArray,
dimensions: dimensions.length > 0 ? dimensions : undefined,
};
}
}
export default StructCollector;
|