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 | 5x 2x 173x 173x 309x 5x 5x 14x 14x 17x 17x 163x 163x 81x 81x 2x 2x 27x 27x 173x 5x 5x 5x 26x 26x 26x 26x 5x 14x 14x 14x 39x 14x 17x 29x 17x 163x 163x 163x 163x 163x 163x 42x 42x 163x 81x 81x 81x 81x 81x 54x 81x 2x 2x 2x 4x 2x 27x | /**
* TSymbolAdapter - Converts TSymbol[] to ISymbol[] for backwards compatibility.
*
* This adapter bridges the gap between the new composable collectors (which return
* rich discriminated union types) and the existing Pipeline/SymbolTable infrastructure
* (which uses flat ISymbol objects).
*
* ADR-055 Phase 3: This enables gradual migration without breaking consumers.
*/
import ISymbol from "../../../../../utils/types/ISymbol";
import ESymbolKind from "../../../../../utils/types/ESymbolKind";
import SymbolTable from "../../SymbolTable";
import TSymbol from "../../types/TSymbol";
import IBitmapSymbol from "../../types/IBitmapSymbol";
import IEnumSymbol from "../../types/IEnumSymbol";
import IStructSymbol from "../../types/IStructSymbol";
import IFunctionSymbol from "../../types/IFunctionSymbol";
import IVariableSymbol from "../../types/IVariableSymbol";
import IRegisterSymbol from "../../types/IRegisterSymbol";
import IScopeSymbol from "../../types/IScopeSymbol";
/** Get minimum unsigned type width for a bit count */
function getMinBitWidth(width: number): number {
if (width <= 8) return 8;
Eif (width <= 16) return 16;
return 32;
}
/**
* Adapts TSymbol[] to ISymbol[] and registers struct fields in SymbolTable.
*/
class TSymbolAdapter {
/**
* Convert TSymbol[] to ISymbol[] for backwards compatibility.
* Also registers struct fields in SymbolTable.
*
* @param symbols Array of discriminated union symbols from collectors
* @param symbolTable SymbolTable for struct field registration and type resolution
* @returns Array of flat ISymbol objects for Pipeline consumption
*/
static toISymbols(symbols: TSymbol[], symbolTable: SymbolTable): ISymbol[] {
const result: ISymbol[] = [];
for (const symbol of symbols) {
switch (symbol.kind) {
case ESymbolKind.Bitmap:
result.push(...TSymbolAdapter.convertBitmap(symbol));
break;
case ESymbolKind.Enum:
result.push(...TSymbolAdapter.convertEnum(symbol));
break;
case ESymbolKind.Struct:
result.push(TSymbolAdapter.convertStruct(symbol, symbolTable));
break;
case ESymbolKind.Function:
result.push(...TSymbolAdapter.convertFunction(symbol));
break;
case ESymbolKind.Variable:
result.push(TSymbolAdapter.convertVariable(symbol));
break;
case ESymbolKind.Register:
result.push(...TSymbolAdapter.convertRegister(symbol));
break;
case ESymbolKind.Namespace:
result.push(TSymbolAdapter.convertScope(symbol));
break;
}
}
return result;
}
/**
* Convert IBitmapSymbol to ISymbol + BitmapField symbols.
*/
private static convertBitmap(bitmap: IBitmapSymbol): ISymbol[] {
const result: ISymbol[] = [];
// Main bitmap symbol
result.push({
name: bitmap.name,
kind: ESymbolKind.Bitmap,
type: bitmap.backingType,
sourceFile: bitmap.sourceFile,
sourceLine: bitmap.sourceLine,
sourceLanguage: bitmap.sourceLanguage,
isExported: bitmap.isExported,
parent: bitmap.parent,
});
// Expand fields to BitmapField symbols
for (const [fieldName, fieldInfo] of bitmap.fields) {
const width = fieldInfo.width;
const bitEnd = fieldInfo.offset + width - 1;
const bitRange =
width === 1
? `bit ${fieldInfo.offset}`
: `bits ${fieldInfo.offset}-${bitEnd}`;
result.push({
name: `${bitmap.name}_${fieldName}`,
kind: ESymbolKind.BitmapField,
type: width === 1 ? "bool" : `u${getMinBitWidth(width)}`,
sourceFile: bitmap.sourceFile,
sourceLine: bitmap.sourceLine,
sourceLanguage: bitmap.sourceLanguage,
isExported: bitmap.isExported,
parent: bitmap.name,
signature: `${bitRange} (${width} bit${width > 1 ? "s" : ""})`,
});
}
return result;
}
/**
* Convert IEnumSymbol to ISymbol + EnumMember symbols for hover support.
*/
private static convertEnum(enumSym: IEnumSymbol): ISymbol[] {
const result: ISymbol[] = [];
// Main enum symbol
result.push({
name: enumSym.name,
kind: ESymbolKind.Enum,
sourceFile: enumSym.sourceFile,
sourceLine: enumSym.sourceLine,
sourceLanguage: enumSym.sourceLanguage,
isExported: enumSym.isExported,
parent: enumSym.parent,
});
// Create EnumMember symbols for hover/autocomplete
// Note: sourceLine points to the enum declaration — IEnumSymbol.members
// is Map<string, number> (name → value) with no per-member line info.
for (const [memberName, memberValue] of enumSym.members) {
result.push({
name: memberName,
kind: ESymbolKind.EnumMember,
type: String(memberValue),
sourceFile: enumSym.sourceFile,
sourceLine: enumSym.sourceLine,
sourceLanguage: enumSym.sourceLanguage,
isExported: enumSym.isExported,
parent: enumSym.name,
});
}
return result;
}
/**
* Convert IStructSymbol to ISymbol and register fields in SymbolTable.
*/
private static convertStruct(
struct: IStructSymbol,
symbolTable: SymbolTable,
): ISymbol {
// Register struct fields in SymbolTable for TypeResolver.isStructType()
for (const [fieldName, fieldInfo] of struct.fields) {
symbolTable.addStructField(
struct.name,
fieldName,
fieldInfo.type,
fieldInfo.dimensions,
);
}
return {
name: struct.name,
kind: ESymbolKind.Struct,
sourceFile: struct.sourceFile,
sourceLine: struct.sourceLine,
sourceLanguage: struct.sourceLanguage,
isExported: struct.isExported,
parent: struct.parent,
};
}
/**
* Convert IFunctionSymbol to ISymbol + parameter symbols for hover support.
*/
private static convertFunction(func: IFunctionSymbol): ISymbol[] {
const result: ISymbol[] = [];
// Build parameter types for signature
const paramTypes = func.parameters.map((p) => p.type);
const signature = `${func.returnType} ${func.name}(${paramTypes.join(", ")})`;
// Build parameter info for header generation
const parameters = func.parameters.map((p) => ({
name: p.name,
type: p.type,
isConst: p.isConst,
isArray: p.isArray,
arrayDimensions: p.arrayDimensions,
isAutoConst: p.isAutoConst,
}));
// Main function symbol
result.push({
name: func.name,
kind: ESymbolKind.Function,
type: func.returnType,
sourceFile: func.sourceFile,
sourceLine: func.sourceLine,
sourceLanguage: func.sourceLanguage,
isExported: func.isExported,
parent: func.parent,
signature,
parameters,
});
// Create parameter symbols for hover support
for (const param of func.parameters) {
const displayType = param.isArray ? `${param.type}[]` : param.type;
result.push({
name: param.name,
kind: ESymbolKind.Variable,
type: displayType,
sourceFile: func.sourceFile,
sourceLine: func.sourceLine,
sourceLanguage: func.sourceLanguage,
isExported: false,
parent: func.name,
});
}
return result;
}
/**
* Convert IVariableSymbol to ISymbol.
*/
private static convertVariable(variable: IVariableSymbol): ISymbol {
// Convert dimensions to string dimensions for ISymbol
const arrayDimensions = variable.arrayDimensions?.map(String);
// Get first dimension for legacy size field (only if numeric)
const firstDim = variable.arrayDimensions?.[0];
const size = typeof firstDim === "number" ? firstDim : undefined;
const result: ISymbol = {
name: variable.name,
kind: ESymbolKind.Variable,
type: variable.type,
sourceFile: variable.sourceFile,
sourceLine: variable.sourceLine,
sourceLanguage: variable.sourceLanguage,
isExported: variable.isExported,
parent: variable.parent,
isConst: variable.isConst,
isAtomic: variable.isAtomic,
isArray: variable.isArray,
arrayDimensions,
size,
};
// Issue #461: Preserve initialValue for const variables (needed for external array dimension resolution)
if (variable.initialValue !== undefined) {
result.initialValue = variable.initialValue;
}
return result;
}
/**
* Convert IRegisterSymbol to ISymbol + RegisterMember symbols.
*/
private static convertRegister(register: IRegisterSymbol): ISymbol[] {
const result: ISymbol[] = [];
// Main register symbol
result.push({
name: register.name,
kind: ESymbolKind.Register,
sourceFile: register.sourceFile,
sourceLine: register.sourceLine,
sourceLanguage: register.sourceLanguage,
isExported: register.isExported,
parent: register.parent,
});
// Expand members to RegisterMember symbols
for (const [memberName, memberInfo] of register.members) {
result.push({
name: `${register.name}_${memberName}`,
kind: ESymbolKind.RegisterMember,
type: memberInfo.cType,
sourceFile: register.sourceFile,
sourceLine: register.sourceLine,
sourceLanguage: register.sourceLanguage,
isExported: register.isExported,
parent: register.name,
accessModifier: memberInfo.access,
});
}
return result;
}
/**
* Convert IScopeSymbol to ISymbol.
* Note: Scope members are collected as separate symbols by ScopeCollector,
* so we only need to convert the scope itself.
*/
private static convertScope(scope: IScopeSymbol): ISymbol {
return {
name: scope.name,
kind: ESymbolKind.Namespace,
sourceFile: scope.sourceFile,
sourceLine: scope.sourceLine,
sourceLanguage: scope.sourceLanguage,
isExported: scope.isExported,
};
}
}
export default TSymbolAdapter;
|