@@ -91,14 +91,16 @@ export class SafeDsTypeComputer {
91
91
private readonly builtinClasses : SafeDsClasses ;
92
92
private readonly nodeMapper : SafeDsNodeMapper ;
93
93
94
- private readonly typeCache : WorkspaceCache < string , Type > ;
94
+ private readonly coreTypeCache : WorkspaceCache < string , Type > ;
95
+ private readonly nodeTypeCache : WorkspaceCache < string , Type > ;
95
96
96
97
constructor ( services : SafeDsServices ) {
97
98
this . astNodeLocator = services . workspace . AstNodeLocator ;
98
99
this . builtinClasses = services . builtins . Classes ;
99
100
this . nodeMapper = services . helpers . NodeMapper ;
100
101
101
- this . typeCache = new WorkspaceCache ( services . shared ) ;
102
+ this . coreTypeCache = new WorkspaceCache ( services . shared ) ;
103
+ this . nodeTypeCache = new WorkspaceCache ( services . shared ) ;
102
104
}
103
105
104
106
/**
@@ -112,7 +114,7 @@ export class SafeDsTypeComputer {
112
114
const documentUri = getDocument ( node ) . uri . toString ( ) ;
113
115
const nodePath = this . astNodeLocator . getAstNodePath ( node ) ;
114
116
const key = `${ documentUri } ~${ nodePath } ` ;
115
- return this . typeCache . get ( key , ( ) => this . doComputeType ( node ) . unwrap ( ) ) ;
117
+ return this . nodeTypeCache . get ( key , ( ) => this . doComputeType ( node ) . unwrap ( ) ) ;
116
118
}
117
119
118
120
// fun SdsAbstractObject.hasPrimitiveType(): Boolean {
@@ -257,21 +259,21 @@ export class SafeDsTypeComputer {
257
259
private computeTypeOfExpression ( node : SdsExpression ) : Type {
258
260
// Terminal cases
259
261
if ( isSdsBoolean ( node ) ) {
260
- return this . Boolean ( ) ;
262
+ return this . Boolean ;
261
263
} else if ( isSdsFloat ( node ) ) {
262
- return this . Float ( ) ;
264
+ return this . Float ;
263
265
} else if ( isSdsInt ( node ) ) {
264
- return this . Int ( ) ;
266
+ return this . Int ;
265
267
} else if ( isSdsList ( node ) ) {
266
- return this . List ( ) ;
268
+ return this . List ;
267
269
} else if ( isSdsMap ( node ) ) {
268
- return this . Map ( ) ;
270
+ return this . Map ;
269
271
} else if ( isSdsNull ( node ) ) {
270
- return this . NothingOrNull ( ) ;
272
+ return this . NothingOrNull ;
271
273
} else if ( isSdsString ( node ) ) {
272
- return this . String ( ) ;
274
+ return this . String ;
273
275
} else if ( isSdsTemplateString ( node ) ) {
274
- return this . String ( ) ;
276
+ return this . String ;
275
277
}
276
278
277
279
// Recursive cases
@@ -304,21 +306,21 @@ export class SafeDsTypeComputer {
304
306
// Boolean operators
305
307
case 'or' :
306
308
case 'and' :
307
- return this . Boolean ( ) ;
309
+ return this . Boolean ;
308
310
309
311
// Equality operators
310
312
case '==' :
311
313
case '!=' :
312
314
case '===' :
313
315
case '!==' :
314
- return this . Boolean ( ) ;
316
+ return this . Boolean ;
315
317
316
318
// Comparison operators
317
319
case '<' :
318
320
case '<=' :
319
321
case '>=' :
320
322
case '>' :
321
- return this . Boolean ( ) ;
323
+ return this . Boolean ;
322
324
323
325
// Arithmetic operators
324
326
case '+' :
@@ -343,7 +345,7 @@ export class SafeDsTypeComputer {
343
345
} else if ( isSdsPrefixOperation ( node ) ) {
344
346
switch ( node . operator ) {
345
347
case 'not' :
346
- return this . Boolean ( ) ;
348
+ return this . Boolean ;
347
349
case '-' :
348
350
return this . computeTypeOfArithmeticPrefixOperation ( node ) ;
349
351
@@ -378,7 +380,7 @@ export class SafeDsTypeComputer {
378
380
379
381
private computeTypeOfIndexedAccess ( node : SdsIndexedAccess ) : Type {
380
382
const receiverType = this . computeType ( node . receiver ) ;
381
- if ( receiverType . equals ( this . List ( ) ) || receiverType . equals ( this . Map ( ) ) ) {
383
+ if ( receiverType . equals ( this . List ) || receiverType . equals ( this . Map ) ) {
382
384
return this . AnyOrNull ( ) ;
383
385
} else {
384
386
return UnknownType ;
@@ -389,10 +391,10 @@ export class SafeDsTypeComputer {
389
391
const leftOperandType = this . computeType ( node . leftOperand ) ;
390
392
const rightOperandType = this . computeType ( node . rightOperand ) ;
391
393
392
- if ( leftOperandType . equals ( this . Int ( ) ) && rightOperandType . equals ( this . Int ( ) ) ) {
393
- return this . Int ( ) ;
394
+ if ( leftOperandType . equals ( this . Int ) && rightOperandType . equals ( this . Int ) ) {
395
+ return this . Int ;
394
396
} else {
395
- return this . Float ( ) ;
397
+ return this . Float ;
396
398
}
397
399
}
398
400
@@ -426,10 +428,10 @@ export class SafeDsTypeComputer {
426
428
private computeTypeOfArithmeticPrefixOperation ( node : SdsPrefixOperation ) : Type {
427
429
const leftOperandType = this . computeType ( node . operand ) ;
428
430
429
- if ( leftOperandType . equals ( this . Int ( ) ) ) {
430
- return this . Int ( ) ;
431
+ if ( leftOperandType . equals ( this . Int ) ) {
432
+ return this . Int ;
431
433
} else {
432
- return this . Float ( ) ;
434
+ return this . Float ;
433
435
}
434
436
}
435
437
@@ -529,43 +531,46 @@ export class SafeDsTypeComputer {
529
531
// Builtin types
530
532
// -----------------------------------------------------------------------------------------------------------------
531
533
532
- private AnyOrNull ( ) : Type {
534
+ AnyOrNull ( ) : Type {
533
535
return this . createCoreType ( this . builtinClasses . Any , true ) ;
534
536
}
535
537
536
- private Boolean ( ) : Type {
538
+ get Boolean ( ) : Type {
537
539
return this . createCoreType ( this . builtinClasses . Boolean ) ;
538
540
}
539
541
540
- private Float ( ) : Type {
542
+ get Float ( ) : Type {
541
543
return this . createCoreType ( this . builtinClasses . Float ) ;
542
544
}
543
545
544
- private Int ( ) : Type {
546
+ get Int ( ) : Type {
545
547
return this . createCoreType ( this . builtinClasses . Int ) ;
546
548
}
547
549
548
- private List ( ) : Type {
550
+ get List ( ) : Type {
549
551
return this . createCoreType ( this . builtinClasses . List ) ;
550
552
}
551
553
552
- private Map ( ) : Type {
554
+ get Map ( ) : Type {
553
555
return this . createCoreType ( this . builtinClasses . Map ) ;
554
556
}
555
557
556
- private NothingOrNull ( ) : Type {
558
+ get NothingOrNull ( ) : Type {
557
559
return this . createCoreType ( this . builtinClasses . Nothing , true ) ;
558
560
}
559
561
560
- private String ( ) : Type {
562
+ get String ( ) : Type {
561
563
return this . createCoreType ( this . builtinClasses . String ) ;
562
564
}
563
565
564
566
private createCoreType ( coreClass : SdsClass | undefined , isNullable : boolean = false ) : Type {
565
- if ( coreClass ) {
566
- return new ClassType ( coreClass , isNullable ) ;
567
- } /* c8 ignore start */ else {
567
+ /* c8 ignore start */
568
+ if ( ! coreClass ) {
568
569
return UnknownType ;
569
- } /* c8 ignore stop */
570
+ }
571
+ /* c8 ignore stop */
572
+
573
+ const key = `${ coreClass . name } ~${ isNullable } ` ;
574
+ return this . coreTypeCache . get ( key , ( ) => new ClassType ( coreClass , isNullable ) ) ;
570
575
}
571
576
}
0 commit comments