Skip to content

Commit f3ff1ab

Browse files
committed
refactor: add 'declaredType' member to 'Variable'
Single 'type' member is not enough for complex hierarchy, because we have to constantly compute effective types and keep in mind which type to show to user, so we just add separate member 'declaredType' - it stores type that was originaly declared. Now, 'type' member will store effective type and we will work only with it.
1 parent 3884cb0 commit f3ff1ab

File tree

1 file changed

+47
-46
lines changed

1 file changed

+47
-46
lines changed

src/variables.ts

Lines changed: 47 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,12 @@ export abstract class Variable {
682682
* Real variable type (maybe with tag inspection)
683683
*/
684684
type: string;
685+
686+
/**
687+
* Type that was originally declared and should be shown
688+
* in variables view.
689+
*/
690+
declaredType: string;
685691

686692
/**
687693
* Evaluate value of variable. Have different meaning for
@@ -735,13 +741,15 @@ export abstract class Variable {
735741
return this.context.debug;
736742
}
737743

738-
constructor(name: string, value: string, type: string,
744+
constructor(name: string, value: string,
745+
type: string, declaredType: string,
739746
context: ExecContext, frameId: number,
740747
parent: Variable | undefined, logger?: utils.ILogger) {
741748
this.parent = parent;
742749
this.name = name;
743750
this.value = value;
744751
this.type = type;
752+
this.declaredType = declaredType;
745753
this.context = context;
746754
this.frameId = frameId;
747755

@@ -823,9 +831,9 @@ export abstract class Variable {
823831
async getTreeItem(): Promise<vscode.TreeItem> {
824832
try {
825833
return {
826-
label: this.type === ''
834+
label: this.declaredType === ''
827835
? this.name
828-
: `${this.name}: ${this.type}`,
836+
: `${this.name}: ${this.declaredType}`,
829837
description: await this.getDescription(),
830838
collapsibleState: this.isExpandable()
831839
? vscode.TreeItemCollapsibleState.Collapsed
@@ -866,6 +874,8 @@ export abstract class Variable {
866874
static async create(debugVariable: dap.DebugVariable, frameId: number,
867875
context: ExecContext, logger: utils.ILogger,
868876
parent?: Variable) {
877+
878+
const effectiveType = Variable.getRealType(debugVariable.type, context);
869879
/*
870880
* We pass RealVariable (not generic Variable), because if we
871881
* want to use this function - it means we create variable
@@ -877,10 +887,11 @@ export abstract class Variable {
877887
parent,
878888
context,
879889
logger,
890+
type: effectiveType,
891+
declaredType: debugVariable.type,
880892
};
881893

882-
const realType = Variable.getRealType(debugVariable.type, context);
883-
if ( context.debug.isValueStruct(debugVariable, realType)
894+
if ( context.debug.isValueStruct(debugVariable, effectiveType)
884895
|| !context.debug.isValidPointerType(debugVariable)) {
885896

886897
if (context.debug.isNull(debugVariable) &&
@@ -899,7 +910,7 @@ export abstract class Variable {
899910
return new ListNodeVariable('List', args);
900911
}
901912

902-
if (realType === 'bitmapword') {
913+
if (effectiveType === 'bitmapword') {
903914
/* Show bitmapword as bitmask, not integer */
904915
return new BitmapwordVariable(args);
905916
}
@@ -916,19 +927,18 @@ export abstract class Variable {
916927
}
917928
}
918929

930+
/*
931+
* Flexible array members for now recognized as
932+
* non-valid pointers/scalars, but we actually
933+
* can handle them.
934+
*/
919935
if (debugVariable.type.endsWith('[]')) {
920936
if (parent?.type && parent instanceof RealVariable) {
921937
const parentType = Variable.getRealType(parent.type, context);
922938
const specialMember = context.specialMemberRegistry
923939
.getArraySpecialMember(parentType, debugVariable.name);
924940
if (specialMember) {
925-
return new ArraySpecialMember(parent, specialMember, {
926-
...debugVariable,
927-
frameId: frameId,
928-
parent: parent,
929-
context,
930-
logger,
931-
}) as RealVariable;
941+
return new ArraySpecialMember(parent, specialMember, args);
932942
}
933943
}
934944
}
@@ -947,42 +957,36 @@ export abstract class Variable {
947957
const specialMember = context.specialMemberRegistry
948958
.getArraySpecialMember(parent.type, debugVariable.name);
949959
if (specialMember) {
950-
return new ArraySpecialMember(parent, specialMember, {
951-
...debugVariable,
952-
frameId: frameId,
953-
parent: parent,
954-
context,
955-
logger
956-
}) as RealVariable;
960+
return new ArraySpecialMember(parent, specialMember, args);
957961
}
958962
}
959963

960964
/*
961965
* PostgreSQL versions prior 16 do not have Bitmapset Node.
962966
* So handle Bitmapset (with Relids) here.
963967
*/
964-
if (BitmapSetSpecialMember.isBitmapsetType(realType)) {
968+
if (BitmapSetSpecialMember.isBitmapsetType(effectiveType)) {
965969
return new BitmapSetSpecialMember(args);
966970
}
967971

968972
/* NodeTag variables: Node, List, Bitmapset etc.. */
969-
if (context.nodeVarRegistry.isNodeVar(realType)) {
973+
if (context.nodeVarRegistry.isNodeVar(effectiveType)) {
970974
const nodeTagVar = await NodeVariable.createNode(debugVariable, frameId,
971-
context, logger, parent);
975+
context, logger, args);
972976
if (nodeTagVar) {
973977
return nodeTagVar;
974978
}
975979
}
976980

977981
/* 'HTAB *' */
978-
if (utils.getPointersCount(realType) === 1 &&
979-
utils.getStructNameFromType(realType) === 'HTAB') {
982+
if (utils.getPointersCount(effectiveType) === 1 &&
983+
utils.getStructNameFromType(effectiveType) === 'HTAB') {
980984
return new HTABSpecialMember(args);
981985
}
982986

983987
/* Simple hash table (simple hash) */
984-
if (SimplehashMember.looksLikeSimpleHashTable(realType)) {
985-
const entry = context.hashTableTypes.findSimpleHashTableType(realType);
988+
if (SimplehashMember.looksLikeSimpleHashTable(effectiveType)) {
989+
const entry = context.hashTableTypes.findSimpleHashTableType(effectiveType);
986990
if (entry) {
987991
return new SimplehashMember(entry, args);
988992
}
@@ -1205,7 +1209,7 @@ export class VariablesRoot extends Variable {
12051209

12061210
constructor(public topLevelVariables: Variable[],
12071211
context: ExecContext, logger: utils.ILogger) {
1208-
super(VariablesRoot.variableRootName, '', '', context, invalidFrameId,
1212+
super(VariablesRoot.variableRootName, '', '', '', context, invalidFrameId,
12091213
undefined, logger);
12101214
}
12111215

@@ -1218,7 +1222,7 @@ class ScalarVariable extends Variable {
12181222
tooltip?: string;
12191223
constructor(name: string, value: string, type: string, context: ExecContext,
12201224
logger: utils.ILogger, parent?: Variable, tooltip?: string) {
1221-
super(name, value, type, context, invalidFrameId, parent, logger);
1225+
super(name, value, type, type, context, invalidFrameId, parent, logger);
12221226
this.tooltip = tooltip;
12231227
}
12241228

@@ -1244,6 +1248,7 @@ interface RealVariableArgs {
12441248
memoryReference?: string;
12451249
name: string;
12461250
type: string;
1251+
declaredType: string;
12471252
value: string;
12481253
variablesReference: number;
12491254
frameId: number;
@@ -1291,7 +1296,8 @@ export class RealVariable extends Variable {
12911296
members?: Variable[];
12921297

12931298
constructor(args: RealVariableArgs) {
1294-
super(args.name, args.value, args.type, args.context, args.frameId, args.parent, args.logger);
1299+
super(args.name, args.value, args.type, args.declaredType,
1300+
args.context, args.frameId, args.parent, args.logger);
12951301
this.memoryReference = args.memoryReference;
12961302
this.variablesReference = args.variablesReference;
12971303
this.parent = args.parent;
@@ -1302,6 +1308,7 @@ export class RealVariable extends Variable {
13021308
memoryReference: this.memoryReference,
13031309
name: this.name,
13041310
type: this.type,
1311+
declaredType: this.declaredType,
13051312
value: this.value,
13061313
variablesReference: this.variablesReference,
13071314
frameId: this.frameId,
@@ -1535,6 +1542,7 @@ export class RealVariable extends Variable {
15351542
}
15361543

15371544
protected formatWatchExpression(myType: string) {
1545+
/* TODO: needs refactoring */
15381546
if (this.parent instanceof VariablesRoot) {
15391547
/* Top level variable */
15401548
if (this.debug.isValueStruct(this, myType)) {
@@ -1676,8 +1684,8 @@ export class NodeVariable extends RealVariable {
16761684
try {
16771685
return {
16781686
label: this.tagsMatch()
1679-
? `${this.name}: ${this.type}`
1680-
: `${this.name}: ${this.type} [${this.realNodeTag}]`,
1687+
? `${this.name}: ${this.declaredType}`
1688+
: `${this.name}: ${this.declaredType} [${this.realNodeTag}]`,
16811689
description: await this.getDescription(),
16821690
collapsibleState: this.isExpandable()
16831691
? vscode.TreeItemCollapsibleState.Collapsed
@@ -1785,7 +1793,7 @@ export class NodeVariable extends RealVariable {
17851793

17861794
static async createNode(variable: dap.DebugVariable, frameId: number,
17871795
context: ExecContext, logger: utils.ILogger,
1788-
parent?: Variable) {
1796+
args: RealVariableArgs) {
17891797
const getRealNodeTag = async () => {
17901798
const expr = `((Node*)(${context.debug.getPointer(variable)}))->type`;
17911799
const response = await context.debug.evaluate(expr, frameId);
@@ -1805,14 +1813,6 @@ export class NodeVariable extends RealVariable {
18051813
return;
18061814
}
18071815

1808-
const args: RealVariableArgs = {
1809-
...variable,
1810-
frameId,
1811-
parent,
1812-
context,
1813-
logger,
1814-
};
1815-
18161816
realTag = realTag.replace('T_', '');
18171817

18181818
/* List */
@@ -3669,6 +3669,7 @@ class ListElementsMember extends RealVariable {
36693669
elements.push(new RealVariable({
36703670
name: `[${i}]` /* array elements behaviour */,
36713671
type: this.listCellType,
3672+
declaredType: this.listCellType,
36723673
variablesReference: response.variablesReference,
36733674
value: response.result,
36743675
memoryReference: response.memoryReference,
@@ -3726,7 +3727,7 @@ class LinkedListElementsMember extends Variable {
37263727

37273728
constructor(listParent: ListNodeVariable, cellValue: string,
37283729
realType: string, context: ExecContext) {
3729-
super('$elements$', '', '', context, listParent.frameId, listParent);
3730+
super('$elements$', '', '', '', context, listParent.frameId, listParent);
37303731
this.listParent = listParent;
37313732
this.cellValue = cellValue;
37323733
this.realType = realType;
@@ -4396,7 +4397,7 @@ class BitmapSetSpecialMember extends NodeVariable {
43964397
value: number,
43974398
context: ExecContext,
43984399
ref: constants.BitmapsetReference | undefined) {
4399-
super(`[${index}]`, value.toString(), '', context, parent.frameId, parent);
4400+
super(`[${index}]`, value.toString(), '', '', context, parent.frameId, parent);
44004401
this.relid = value;
44014402
this.bmsParent = bmsParent;
44024403
this.ref = ref;
@@ -4557,7 +4558,7 @@ class BitmapSetSpecialMember extends NodeVariable {
45574558
constructor(parent: BitmapSetSpecialMember,
45584559
setElements: number[],
45594560
private ref?: constants.BitmapsetReference) {
4560-
super('$elements$', '', '', parent.context, parent.frameId, parent);
4561+
super('$elements$', '', '', '', parent.context, parent.frameId, parent);
45614562
this.setElements = setElements;
45624563
this.bmsParent = parent;
45634564
}
@@ -4881,7 +4882,7 @@ class HTABElementsMember extends Variable {
48814882
entryType: string;
48824883

48834884
constructor(htab: HTABSpecialMember, entryType: string) {
4884-
super('$elements$', '', '', htab.context, htab.frameId, htab, htab.logger);
4885+
super('$elements$', '', '', '', htab.context, htab.frameId, htab, htab.logger);
48854886
this.htab = htab;
48864887
this.entryType = entryType;
48874888
}
@@ -5107,7 +5108,7 @@ class SimplehashElementsMember extends Variable {
51075108
hashTable: SimplehashMember;
51085109

51095110
constructor(hashTable: SimplehashMember) {
5110-
super('$elements$', '', '', hashTable.context, hashTable.frameId,
5111+
super('$elements$', '', '', '', hashTable.context, hashTable.frameId,
51115112
hashTable, hashTable.logger);
51125113
this.hashTable = hashTable;
51135114
}

0 commit comments

Comments
 (0)