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 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 | 1158x 1158x 1158x 1158x 1158x 1158x 1158x 1158x 1158x 1915x 496x 1915x 197x 582x 34x 34x 34x 34x 22x 22x 22x 3x 197x 197x 299x 86x 1158x 1158x 1915x 19x 19x 19x 19x 1915x 197x 197x 197x 197x 299x 2x 2x 2x 2x 2x 299x 13x 13x 13x 1158x 1915x 19x 1896x 1896x 1896x 197x 197x 1699x 161x 161x 161x 1538x 72x 72x 72x 1466x 11x 11x 11x 1455x 941x 941x 941x 941x 941x 514x 496x 496x 197x 197x 197x 296x 15x 281x | /**
* CNextResolver - Orchestrates symbol collection from C-Next parse trees.
* Uses two-pass collection to handle forward references (bitmaps before registers).
*
* Produces TType-based symbols with proper IScopeSymbol references.
*/
import * as Parser from "../../parser/grammar/CNextParser";
import TSymbol from "../../../types/symbols/TSymbol";
import SymbolRegistry from "../../../state/SymbolRegistry";
import LiteralUtils from "../../../../utils/LiteralUtils";
import BitmapCollector from "./collectors/BitmapCollector";
import EnumCollector from "./collectors/EnumCollector";
import StructCollector from "./collectors/StructCollector";
import FunctionCollector from "./collectors/FunctionCollector";
import VariableCollector from "./collectors/VariableCollector";
import RegisterCollector from "./collectors/RegisterCollector";
import ScopeCollector from "./collectors/ScopeCollector";
class CNextResolver {
/**
* Resolve all symbols from a C-Next program parse tree.
*
* @param tree The program context from the parser
* @param sourceFile Source file path
* @param externalConstValues Optional map of const values from external files (e.g., #included .cnx files)
* @returns Array of all collected symbols
*/
static resolve(
tree: Parser.ProgramContext,
sourceFile: string,
externalConstValues?: Map<string, number>,
): TSymbol[] {
const symbols: TSymbol[] = [];
const knownBitmaps = new Set<string>();
const constValues = new Map<string, number>();
// Issue #461: Start with external const values from included files
Iif (externalConstValues) {
for (const [name, value] of externalConstValues) {
constValues.set(name, value);
}
}
// Pass 0: Collect const values (needed for resolving array dimensions)
// Local constants override external ones (unlikely but handles shadowing)
CNextResolver.collectConstValuesPass0(tree, constValues);
// Pass 1: Collect all bitmap names (needed before registers reference them)
// This includes bitmaps in scopes
CNextResolver.collectBitmapsPass1(
tree,
sourceFile,
symbols,
knownBitmaps,
constValues,
);
// Pass 2: Collect everything else (with bitmap set and const values available)
CNextResolver.collectAllPass2(
tree,
sourceFile,
symbols,
knownBitmaps,
constValues,
);
return symbols;
}
/**
* Pass 0: Collect const values for resolving array dimensions.
* Only collects simple integer literals - complex expressions are not supported.
*/
private static collectConstValuesPass0(
tree: Parser.ProgramContext,
constValues: Map<string, number>,
): void {
for (const decl of tree.declaration()) {
// Top-level const variables
if (decl.variableDeclaration()) {
CNextResolver._collectConstFromVar(
decl.variableDeclaration()!,
undefined,
constValues,
);
}
// Const variables inside scopes
if (decl.scopeDeclaration()) {
CNextResolver._collectConstFromScope(
decl.scopeDeclaration()!,
constValues,
);
}
}
}
/**
* Collect const value from a single variable declaration
*/
private static _collectConstFromVar(
varCtx: Parser.VariableDeclarationContext,
scopeName: string | undefined,
constValues: Map<string, number>,
): void {
if (!varCtx.constModifier()) return;
const exprCtx = varCtx.expression();
Iif (!exprCtx) return;
const value = LiteralUtils.parseIntegerLiteral(exprCtx.getText());
if (value === undefined) return;
const name = varCtx.IDENTIFIER().getText();
constValues.set(name, value);
// Store scoped name as well for scoped variables
if (scopeName) {
constValues.set(`${scopeName}_${name}`, value);
}
}
/**
* Collect const values from all variables in a scope
*/
private static _collectConstFromScope(
scopeDecl: Parser.ScopeDeclarationContext,
constValues: Map<string, number>,
): void {
const scopeName = scopeDecl.IDENTIFIER().getText();
for (const member of scopeDecl.scopeMember()) {
if (member.variableDeclaration()) {
CNextResolver._collectConstFromVar(
member.variableDeclaration()!,
scopeName,
constValues,
);
}
}
}
/**
* Pass 1: Collect all bitmaps (including those in scopes).
* Also collects structs in scopes early for type availability.
* SonarCloud S3776: Refactored to use helper methods.
*/
private static collectBitmapsPass1(
tree: Parser.ProgramContext,
sourceFile: string,
symbols: TSymbol[],
knownBitmaps: Set<string>,
constValues: Map<string, number>,
): void {
const globalScope = SymbolRegistry.getGlobalScope();
for (const decl of tree.declaration()) {
// Top-level bitmaps
if (decl.bitmapDeclaration()) {
const bitmapCtx = decl.bitmapDeclaration()!;
const symbol = BitmapCollector.collect(
bitmapCtx,
sourceFile,
globalScope,
);
symbols.push(symbol);
// Use transpiled C name (global bitmaps have no scope prefix)
knownBitmaps.add(symbol.name);
}
// Bitmaps and structs inside scopes (collected early)
if (decl.scopeDeclaration()) {
CNextResolver.collectScopedBitmapsAndStructs(
decl.scopeDeclaration()!,
sourceFile,
symbols,
knownBitmaps,
constValues,
);
}
}
}
/**
* Collect bitmaps and structs from within a scope declaration.
* SonarCloud S3776: Extracted from collectBitmapsPass1().
*/
private static collectScopedBitmapsAndStructs(
scopeDecl: Parser.ScopeDeclarationContext,
sourceFile: string,
symbols: TSymbol[],
knownBitmaps: Set<string>,
constValues: Map<string, number>,
): void {
const scopeName = scopeDecl.IDENTIFIER().getText();
const scope = SymbolRegistry.getOrCreateScope(scopeName);
for (const member of scopeDecl.scopeMember()) {
if (member.bitmapDeclaration()) {
const bitmapCtx = member.bitmapDeclaration()!;
const symbol = BitmapCollector.collect(bitmapCtx, sourceFile, scope);
symbols.push(symbol);
// Use transpiled C name (e.g., "Timer_ControlBits") for scoped bitmaps
const cName = `${scopeName}_${symbol.name}`;
knownBitmaps.add(cName);
}
// Collect structs early so they're available as types
if (member.structDeclaration()) {
const structCtx = member.structDeclaration()!;
const symbol = StructCollector.collect(
structCtx,
sourceFile,
scope,
constValues,
);
symbols.push(symbol);
}
}
}
/**
* Pass 2: Collect all remaining symbols.
* Bitmaps and scoped structs were already collected in pass 1.
*/
private static collectAllPass2(
tree: Parser.ProgramContext,
sourceFile: string,
symbols: TSymbol[],
knownBitmaps: Set<string>,
constValues: Map<string, number>,
): void {
for (const decl of tree.declaration()) {
// Skip bitmaps (already collected in pass 1)
if (decl.bitmapDeclaration()) {
continue;
}
CNextResolver._collectDeclaration(
decl,
sourceFile,
symbols,
knownBitmaps,
constValues,
);
}
}
/**
* Collect symbols from a single declaration.
*/
private static _collectDeclaration(
decl: Parser.DeclarationContext,
sourceFile: string,
symbols: TSymbol[],
knownBitmaps: Set<string>,
constValues: Map<string, number>,
): void {
const globalScope = SymbolRegistry.getGlobalScope();
// Scopes (ScopeCollector handles nested members)
if (decl.scopeDeclaration()) {
CNextResolver._collectScopeDeclaration(
decl.scopeDeclaration()!,
sourceFile,
symbols,
knownBitmaps,
constValues,
);
return;
}
// Top-level structs
if (decl.structDeclaration()) {
const symbol = StructCollector.collect(
decl.structDeclaration()!,
sourceFile,
globalScope,
constValues,
);
symbols.push(symbol);
return;
}
// Top-level enums
if (decl.enumDeclaration()) {
const symbol = EnumCollector.collect(
decl.enumDeclaration()!,
sourceFile,
globalScope,
);
symbols.push(symbol);
return;
}
// Top-level registers
if (decl.registerDeclaration()) {
const symbol = RegisterCollector.collect(
decl.registerDeclaration()!,
sourceFile,
knownBitmaps,
globalScope,
);
symbols.push(symbol);
return;
}
// Top-level functions
if (decl.functionDeclaration()) {
const funcDecl = decl.functionDeclaration()!;
const body = funcDecl.block();
// Use collectAndRegister to populate both old symbols and SymbolRegistry
const symbol = FunctionCollector.collectAndRegister(
funcDecl,
sourceFile,
undefined, // global scope name (empty string)
body,
"private", // default visibility for global functions
);
symbols.push(symbol);
return;
}
// Top-level variables
if (decl.variableDeclaration()) {
const symbol = VariableCollector.collect(
decl.variableDeclaration()!,
sourceFile,
globalScope,
true,
constValues,
);
symbols.push(symbol);
}
}
/**
* Collect scope declaration and its non-bitmap/non-struct members.
*/
private static _collectScopeDeclaration(
scopeCtx: Parser.ScopeDeclarationContext,
sourceFile: string,
symbols: TSymbol[],
knownBitmaps: Set<string>,
constValues: Map<string, number>,
): void {
const result = ScopeCollector.collect(
scopeCtx,
sourceFile,
knownBitmaps,
constValues,
);
symbols.push(result.scopeSymbol);
// Add member symbols, but skip bitmaps and structs (already collected in pass 1)
for (const memberSymbol of result.memberSymbols) {
if (memberSymbol.kind === "bitmap" || memberSymbol.kind === "struct") {
continue;
}
symbols.push(memberSymbol);
}
}
}
export default CNextResolver;
|