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 | 207x 207x 207x 207x 207x 207x 207x 207x 207x 207x 207x 207x 311x 311x 311x 311x 311x 311x 89x 89x 89x 89x 89x 89x 311x 162x 162x 162x 162x 162x 162x 162x 311x 32x 32x 32x 32x 32x 32x 311x 3x 3x 3x 3x 3x 3x 311x 14x 14x 14x 14x 14x 14x 311x 8x 8x 8x 8x 8x 8x 207x | /**
* ScopeCollector - Extracts scope declarations and their nested members.
* ADR-016: Scopes group related functions and control member visibility.
*
* Produces TType-based symbols with proper IScopeSymbol references.
* Uses SymbolRegistry for scope management.
*/
import * as Parser from "../../../parser/grammar/CNextParser";
import ESourceLanguage from "../../../../../utils/types/ESourceLanguage";
import ScopeUtils from "../../../../../utils/ScopeUtils";
import TSymbol from "../../../../types/symbols/TSymbol";
import TVisibility from "../../../../types/TVisibility";
import IScopeCollectorResult from "../types/IScopeCollectorResult";
import SymbolRegistry from "../../../../state/SymbolRegistry";
import BitmapCollector from "./BitmapCollector";
import EnumCollector from "./EnumCollector";
import StructCollector from "./StructCollector";
import FunctionCollector from "./FunctionCollector";
import VariableCollector from "./VariableCollector";
import RegisterCollector from "./RegisterCollector";
class ScopeCollector {
/**
* Collect a scope declaration and all its nested members.
*
* Uses SymbolRegistry to get/create the scope, ensuring proper scope
* references in all member symbols.
*
* **Side-effect**: This method calls SymbolRegistry.getOrCreateScope(),
* which creates the scope in global state if it doesn't exist. Tests
* should call SymbolRegistry.reset() in beforeEach to ensure isolation.
*
* @param ctx The scope declaration context
* @param sourceFile Source file path
* @param knownBitmaps Set of known bitmap type names for register resolution
* @param constValues Map of constant names to their numeric values (for resolving array dimensions)
* @returns The scope symbol and all member symbols
*/
static collect(
ctx: Parser.ScopeDeclarationContext,
sourceFile: string,
knownBitmaps: Set<string>,
constValues?: Map<string, number>,
): IScopeCollectorResult {
const scopeName = ctx.IDENTIFIER().getText();
const line = ctx.start?.line ?? 0;
// Get or create the scope via SymbolRegistry
const scope = SymbolRegistry.getOrCreateScope(scopeName);
// Update scope metadata (cast to mutable for initialization)
const mutableScope = scope as {
sourceFile: string;
sourceLine: number;
sourceLanguage: ESourceLanguage;
isExported: boolean;
};
mutableScope.sourceFile = sourceFile;
mutableScope.sourceLine = line;
mutableScope.sourceLanguage = ESourceLanguage.CNext;
mutableScope.isExported = true;
// Cast readonly collections to mutable (scope is being populated)
const memberVisibility = scope.memberVisibility as unknown as Map<
string,
TVisibility
>;
const members = scope.members as unknown as string[];
const memberSymbols: TSymbol[] = [];
for (const member of ctx.scopeMember()) {
// ADR-016: Extract visibility with member-type-aware defaults
const visibilityMod = member.visibilityModifier();
const explicitVisibility = visibilityMod?.getText() as
| TVisibility
| undefined;
const isFunction = member.functionDeclaration() !== null;
const visibility: TVisibility =
explicitVisibility ?? ScopeUtils.getDefaultVisibility(isFunction);
const isPublic = visibility === "public";
// Handle variable declarations
if (member.variableDeclaration()) {
const varDecl = member.variableDeclaration()!;
const varName = varDecl.IDENTIFIER().getText();
memberVisibility.set(varName, visibility);
members.push(varName);
const varSymbol = VariableCollector.collect(
varDecl,
sourceFile,
scope,
isPublic,
constValues,
);
memberSymbols.push(varSymbol);
}
// Handle function declarations
if (member.functionDeclaration()) {
const funcDecl = member.functionDeclaration()!;
const funcName = funcDecl.IDENTIFIER().getText();
memberVisibility.set(funcName, visibility);
members.push(funcName);
// Use collectAndRegister to populate both memberSymbols and SymbolRegistry
const body = funcDecl.block();
const funcSymbol = FunctionCollector.collectAndRegister(
funcDecl,
sourceFile,
scopeName,
body,
visibility,
);
memberSymbols.push(funcSymbol);
}
// Handle enum declarations
if (member.enumDeclaration()) {
const enumDecl = member.enumDeclaration()!;
const enumName = enumDecl.IDENTIFIER().getText();
memberVisibility.set(enumName, visibility);
members.push(enumName);
const enumSymbol = EnumCollector.collect(enumDecl, sourceFile, scope);
memberSymbols.push(enumSymbol);
}
// Handle bitmap declarations
if (member.bitmapDeclaration()) {
const bitmapDecl = member.bitmapDeclaration()!;
const bitmapName = bitmapDecl.IDENTIFIER().getText();
memberVisibility.set(bitmapName, visibility);
members.push(bitmapName);
const bitmapSymbol = BitmapCollector.collect(
bitmapDecl,
sourceFile,
scope,
);
memberSymbols.push(bitmapSymbol);
}
// Handle struct declarations
if (member.structDeclaration()) {
const structDecl = member.structDeclaration()!;
const structName = structDecl.IDENTIFIER().getText();
memberVisibility.set(structName, visibility);
members.push(structName);
const structSymbol = StructCollector.collect(
structDecl,
sourceFile,
scope,
constValues,
);
memberSymbols.push(structSymbol);
}
// Handle register declarations
if (member.registerDeclaration()) {
const regDecl = member.registerDeclaration()!;
const regName = regDecl.IDENTIFIER().getText();
memberVisibility.set(regName, visibility);
members.push(regName);
const regSymbol = RegisterCollector.collect(
regDecl,
sourceFile,
knownBitmaps,
scope,
);
memberSymbols.push(regSymbol);
}
}
return { scopeSymbol: scope, memberSymbols };
}
}
export default ScopeCollector;
|