@@ -14,19 +14,18 @@ private predicate instanceVariableAccess(
14
14
) {
15
15
name = var .getValue ( ) and
16
16
scope = enclosingModuleOrClass ( var ) and
17
- if exists ( enclosingMethod ( var ) ) then instance = true else instance = false
17
+ if hasEnclosingMethod ( var ) then instance = true else instance = false
18
18
}
19
19
20
20
private predicate classVariableAccess ( Generated:: ClassVariable var , string name , VariableScope scope ) {
21
21
name = var .getValue ( ) and
22
22
scope = enclosingModuleOrClass ( var )
23
23
}
24
24
25
- private Callable enclosingMethod ( Generated:: AstNode node ) {
26
- parentCallableScope * ( enclosingScope ( node ) ) = TCallableScope ( result ) and
27
- (
28
- result instanceof Method or
29
- result instanceof SingletonMethod
25
+ predicate hasEnclosingMethod ( Generated:: AstNode node ) {
26
+ exists ( Callable method | parentCallableScope * ( enclosingScope ( node ) ) = TCallableScope ( method ) |
27
+ method instanceof Method or
28
+ method instanceof SingletonMethod
30
29
)
31
30
}
32
31
@@ -36,13 +35,13 @@ private TCallableScope parentCallableScope(TCallableScope scope) {
36
35
not c instanceof Method and
37
36
not c instanceof SingletonMethod
38
37
|
39
- result = outerScope ( scope )
38
+ result = scope . ( VariableScope ) . getOuterScope ( )
40
39
)
41
40
}
42
41
43
42
private VariableScope parentScope ( VariableScope scope ) {
44
43
not scope instanceof ModuleOrClassScope and
45
- result = outerScope ( scope )
44
+ result = scope . getOuterScope ( )
46
45
}
47
46
48
47
private ModuleOrClassScope enclosingModuleOrClass ( Generated:: AstNode node ) {
@@ -96,10 +95,6 @@ private predicate strictlyBefore(Location one, Location two) {
96
95
one .getStartLine ( ) = two .getStartLine ( ) and one .getStartColumn ( ) < two .getStartColumn ( )
97
96
}
98
97
99
- private VariableScope outerScope ( VariableScope scope ) {
100
- result = enclosingScope ( scope .getScopeElement ( ) )
101
- }
102
-
103
98
/** A scope that may capture outer local variables. */
104
99
private class CapturingScope extends VariableScope {
105
100
CapturingScope ( ) {
@@ -112,9 +107,6 @@ private class CapturingScope extends VariableScope {
112
107
)
113
108
}
114
109
115
- /** Gets the scope in which this scope is nested, if any. */
116
- VariableScope getOuterScope ( ) { result = outerScope ( this ) }
117
-
118
110
/** Holds if this scope inherits `name` from an outer scope `outer`. */
119
111
predicate inherits ( string name , VariableScope outer ) {
120
112
not scopeDefinesParameterVariable ( this , name , _) and
@@ -365,6 +357,22 @@ private module Cached {
365
357
predicate isCapturedAccess ( LocalVariableAccess:: Range access ) {
366
358
access .getVariable ( ) .getDeclaringScope ( ) != enclosingScope ( access )
367
359
}
360
+
361
+ cached
362
+ predicate instanceVariableAccess ( Generated:: InstanceVariable var , InstanceVariable v ) {
363
+ exists ( string name , VariableScope scope , boolean instance |
364
+ v = TInstanceVariable ( scope , name , instance , _) and
365
+ instanceVariableAccess ( var , name , scope , instance )
366
+ )
367
+ }
368
+
369
+ cached
370
+ predicate classVariableAccess ( Generated:: ClassVariable var , ClassVariable variable ) {
371
+ exists ( VariableScope scope , string name |
372
+ variable = TClassVariable ( scope , name , _) and
373
+ classVariableAccess ( var , name , scope )
374
+ )
375
+ }
368
376
}
369
377
370
378
import Cached
@@ -551,12 +559,7 @@ module InstanceVariableAccess {
551
559
class Range extends VariableAccess:: Range , @token_instance_variable {
552
560
InstanceVariable variable ;
553
561
554
- Range ( ) {
555
- exists ( boolean instance , VariableScope scope , string name |
556
- variable = TInstanceVariable ( scope , name , instance , _) and
557
- instanceVariableAccess ( this , name , scope , instance )
558
- )
559
- }
562
+ Range ( ) { instanceVariableAccess ( this , variable ) }
560
563
561
564
final override InstanceVariable getVariable ( ) { result = variable }
562
565
}
@@ -566,12 +569,7 @@ module ClassVariableAccess {
566
569
class Range extends VariableAccess:: Range , @token_class_variable {
567
570
ClassVariable variable ;
568
571
569
- Range ( ) {
570
- exists ( VariableScope scope , string name |
571
- variable = TClassVariable ( scope , name , _) and
572
- classVariableAccess ( this , name , scope )
573
- )
574
- }
572
+ Range ( ) { classVariableAccess ( this , variable ) }
575
573
576
574
final override ClassVariable getVariable ( ) { result = variable }
577
575
}
0 commit comments