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 | 374x 370x 4x 4x 4x 4x 4x 4x 4x 4x 3x 3x 3x 3x 2x 3x 3x 25x 19x 21x 21x 21x 229x 229x 229x 229x 374x 374x 374x 229x 374x 374x 374x 374x 374x 374x 374x 374x 374x 374x 4x 4x 4x 374x 3x 3x 3x 371x 17x 17x 374x | /**
* 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 DimensionResolver from "../utils/DimensionResolver";
import ScopeUtils from "../../../../../utils/ScopeUtils";
/**
* Result of processing an arrayType syntax context.
*/
interface IArrayTypeResult {
isArray: boolean;
/**
* Every dimension, or undefined when the count is not knowable here -- no
* dimensions at all, or an unsized `[]`.
*
* A dimension that does not fold is NOT dropped: DimensionResolver carries
* it as source text, and qualifyStructFieldDimensions resolves it later. So
* `u8[EColor.COUNT][3]` yields ["EColor.COUNT", 3], not undefined.
*
* That resolution covers enum-qualified names. Text naming anything else C
* does not know reaches the header verbatim and does not compile -- #1175.
*
* Position matters more than resolution (issue #1158). A partial list
* silently shifts later dimensions -- `u8[N][3]` reporting [3] makes the
* consumer treat 3 as dimension 1 -- and a truncated list is worse than no
* list, because a non-empty list suppresses StructGenerator's AST fallback,
* which resolves all dimensions correctly on its own. Either every slot is
* present, or the list is undefined.
*/
dimensions: (number | string)[] | 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, dimensions: undefined };
}
// Issue #1158: read every dimension. This previously took dims[0] only, so
// `u8[2][3] cells` was recorded as [2]; that list is non-empty, so it won
// over StructGenerator's AST fallback and both the .c and the .h emitted
// `uint8_t cells[2]` while the body still emitted `cells[1][2]`.
const dims = arrayTypeCtx.arrayTypeDimension();
Iif (dims.length === 0) {
return { isArray: true, dimensions: undefined };
}
const dimensions: (number | string)[] = [];
for (const dim of dims) {
const sizeExpr = dim.expression();
Iif (!sizeExpr) {
// Unsized `[]` -- size is not knowable here.
return { isArray: true, dimensions: undefined };
}
// Always a number or the source text -- never undefined -- so every slot
// is filled and positions are preserved.
dimensions.push(tryResolveExpressionDimension(sizeExpr, constValues));
}
return { isArray: true, dimensions };
}
/**
* 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 | string {
return DimensionResolver.resolve(sizeExpr, constValues);
}
/**
* 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) {
dimensions.push(tryResolveExpressionDimension(sizeExpr, constValues));
}
}
}
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)
* @param isScopeType ADR-057 predicate: is this *qualified* name a scope type?
* @returns The struct symbol with TType-based types and scope reference
*/
static collect(
ctx: Parser.StructDeclarationContext,
sourceFile: string,
scope: IScopeSymbol,
constValues?: Map<string, number>,
isScopeType?: (qualifiedName: string) => boolean,
): IStructSymbol {
const name = ctx.IDENTIFIER().getText();
const line = ctx.start?.line ?? 0;
// #1285: the scope REFERENCE flows on from here. Flattening it to its
// leaf name was the choke point that made every downstream qualification
// one level deep, whatever the chain actually was.
const fields = new Map<string, IFieldInfo>();
for (const member of ctx.structMember()) {
const fieldName = member.IDENTIFIER().getText();
const fieldInfo = StructCollector.collectField(
member,
fieldName,
scope,
constValues,
isScopeType,
);
fields.set(fieldName, fieldInfo);
}
return {
kind: "struct",
name,
scope,
// #1285: identity computed once, from the scope chain, not
// re-derived by every consumer.
...ScopeUtils.identityOf({ 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,
scope?: IScopeSymbol,
constValues?: Map<string, number>,
isScopeType?: (qualifiedName: string) => boolean,
): IFieldInfo {
const typeCtx = member.type();
const fieldTypeStr = TypeUtils.getTypeName(typeCtx, scope, isScopeType);
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.dimensions !== undefined) {
dimensions.push(...arrayTypeResult.dimensions);
}
// dimensions is undefined only for an unsized `[]` or no dimensions at
// all; an expression that does not fold (global.EnumName.COUNT) is
// carried as source text and resolved by qualifyStructFieldDimensions.
}
// 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;
|