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 | 968x 905x 63x 63x 63x 63x 64x 64x 1x 63x 62x 10x 10x 10x 10x 2x 10x 10x 31x 33x 33x 32x 602x 602x 602x 602x 602x 602x 968x 968x 968x 602x 968x 968x 968x 968x 968x 968x 968x 968x 968x 63x 63x 62x 968x 10x 10x 10x 958x 29x 29x 968x 105x 105x | /**
* 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 "../../../2-Parse/grammar/CNextParser";
import ESourceLanguage from "../../../../utils/types/ESourceLanguage";
import IStructSymbol from "../../../../types/symbols/IStructSymbol";
import type IStructFieldSymbol from "../../../../types/symbols/IStructFieldSymbol";
import TypeUtils from "../utils/TypeUtils";
import DimensionResolver from "../utils/DimensionResolver";
import type IDeclaredDimension from "../types/IDeclaredDimension";
import ScopeUtils from "../../../../utils/ScopeUtils";
import TVisibility from "../../../../types/TVisibility";
import ParserUtils from "../../../../utils/ParserUtils";
import MemberSymbolBase from "../utils/MemberSymbolBase";
import type ISourceSpan from "../../../../types/ISourceSpan";
/**
* 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 here is NOT dropped: DimensionResolver
* keeps it as written, and 1.4 Resolve settles it. So `u8[EColor.COUNT][3]`
* yields two slots, not undefined. (#1175: it used to keep the source TEXT,
* which reached the header naming what C cannot see.)
*
* 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: IDeclaredDimension[] | undefined;
}
/**
* Process arrayType syntax (e.g., Item[3] items) and return array info.
*/
function processArrayTypeSyntax(
arrayTypeCtx: Parser.ArrayTypeContext | null | undefined,
): 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: IDeclaredDimension[] = [];
for (const dim of dims) {
const sizeExpr = dim.expression();
if (!sizeExpr) {
// Unsized `[]` -- size is not knowable here.
return { isArray: true, dimensions: undefined };
}
// Always a size or the dimension as written -- never undefined -- so every
// slot is filled and positions are preserved.
dimensions.push(DimensionResolver.resolve(sizeExpr));
}
return { isArray: true, dimensions };
}
/**
* Process string type fields and update dimensions array.
*/
function processStringField(
stringCtx: Parser.StringTypeContext,
arrayDims: Parser.ArrayDimensionContext[],
dimensions: IDeclaredDimension[],
): 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);
}
// String capacity becomes final dimension (+1 for null terminator)
dimensions.push({ size: capacity + 1, expr: null });
return true;
}
/**
* Parse array dimension expressions and append resolved sizes to dimensions array.
*/
function parseArrayDimensions(
arrayDims: Parser.ArrayDimensionContext[],
dimensions: IDeclaredDimension[],
): void {
for (const dim of arrayDims) {
const sizeExpr = dim.expression();
if (sizeExpr) {
dimensions.push(DimensionResolver.resolve(sizeExpr));
}
}
}
/**
* The declaring struct's facts, as one parameter.
*
* #1318 added three arguments to `collectField` -- the owner's scoped name,
* the source file and the inherited visibility -- taking it to 8 against a
* limit of 7. They are not three independent knobs: they are one answer to
* "which struct is this field part of", so they travel together.
*/
interface IFieldOwner {
readonly scopedName: string;
readonly sourceFile: string;
readonly visibility: TVisibility;
/** The struct's own span, inherited by a field that has no start token. */
readonly span: ISourceSpan;
}
class StructCollector {
/**
* Collect a struct declaration and return an IStructSymbol.
*
* @param ctx The struct declaration context
* @param sourceFile Source file path
* @param scopePath The path of the scope this struct belongs to (dotted path, "" at file scope)
* @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,
scopePath: string,
visibility: TVisibility,
isScopeType?: (qualifiedName: string) => boolean,
): IStructSymbol {
const name = ctx.IDENTIFIER().getText();
const span = ParserUtils.getSpan(ctx);
// #1298: members carry the scope's PATH, not the scope object. The path
// holds every outer component, so nothing downstream can flatten it to a
// leaf -- which is what the reference threaded here used to protect against.
const fields = new Map<string, IStructFieldSymbol>();
// #1318: a field hangs off the STRUCT, not the enclosing scope.
const identity = ScopeUtils.identityOf({ name, scopePath });
const ownerScopedName = identity.cnxScopedName;
for (const member of ctx.structMember()) {
const fieldName = member.IDENTIFIER().getText();
const fieldInfo = StructCollector.collectField(
member,
fieldName,
{ scopedName: ownerScopedName, sourceFile, visibility, span },
scopePath,
isScopeType,
);
fields.set(fieldName, fieldInfo);
}
return {
kind: "struct",
name,
scopePath,
// #1285: identity computed once, from the scope chain, not
// re-derived by every consumer.
// #1318 review: the same identity the members were keyed by, not a
// second call with the same arguments -- change one and the members
// would keep the old parent name while this reported the new one.
...identity,
sourceFile,
span,
sourceLanguage: ESourceLanguage.CNext,
visibility,
fields,
};
}
/**
* Collect a single struct field and return its symbol.
*
* #1318: a field is a symbol, so it carries its OWN span -- a struct
* declared across twenty lines used to give every field the struct's
* position, or none at all.
*/
private static collectField(
member: Parser.StructMemberContext,
fieldName: string,
owner: IFieldOwner,
scopePath = "",
isScopeType?: (qualifiedName: string) => boolean,
): IStructFieldSymbol {
const typeCtx = member.type();
const fieldType = TypeUtils.resolveType(typeCtx, scopePath, isScopeType);
// 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: IDeclaredDimension[] = [];
let isArray = false;
// Check for C-Next style arrayType syntax: Item[3] items -> typeCtx.arrayType()
const arrayTypeResult = processArrayTypeSyntax(typeCtx.arrayType());
if (arrayTypeResult.isArray) {
isArray = true;
if (arrayTypeResult.dimensions !== undefined) {
dimensions.push(...arrayTypeResult.dimensions);
}
// dimensions is undefined only for an unsized `[]` or no dimensions at
// all; one that does not fold here (global.EnumName.COUNT) is kept as
// written and settled by 1.4 Resolve.
}
// Handle string types specially
if (typeCtx.stringType()) {
const stringHandled = processStringField(
typeCtx.stringType()!,
arrayDims,
dimensions,
);
Eif (stringHandled) {
isArray = true;
}
} else if (arrayDims.length > 0) {
// Non-string array
isArray = true;
parseArrayDimensions(arrayDims, dimensions);
}
return {
...MemberSymbolBase.of({
kind: "struct_field" as const,
name: fieldName,
parentScopedName: owner.scopedName,
memberCtx: member,
parentSpan: owner.span,
sourceFile: owner.sourceFile,
visibility: owner.visibility,
}),
type: fieldType,
isConst,
isAtomic,
isArray,
dimensions:
dimensions.length > 0 ? dimensions.map((dim) => dim.size) : undefined,
dimensionExprs:
dimensions.length > 0 ? dimensions.map((dim) => dim.expr) : undefined,
};
}
}
export default StructCollector;
|