@@ -13,24 +13,54 @@ private VariableScope enclosingScope(AstNode node) {
1313 result .getScopeElement ( ) = parent * ( node .getParent ( ) )
1414}
1515
16- /** Holds if `scope` defines `name` as a parameter. */
16+ /** A parameter. */
17+ class Parameter extends AstNode {
18+ private int position ;
19+ private VariableScope scope ;
20+
21+ Parameter ( ) {
22+ this =
23+ scope .( BlockScope ) .getScopeElement ( ) .getAFieldOrChild ( ) .( BlockParameters ) .getChild ( position )
24+ or
25+ this =
26+ scope .( MethodScope ) .getScopeElement ( ) .getAFieldOrChild ( ) .( MethodParameters ) .getChild ( position )
27+ }
28+
29+ /** Gets the (zero-based) position of this parameter. */
30+ final int getPosition ( ) { result = position }
31+
32+ /** Gets the scope this parameter is declared in. */
33+ final VariableScope getDeclaringScope ( ) { result = scope }
34+
35+ /** Gets an access to this parameter. */
36+ final ParameterAccess getAnAccess ( ) { result .getParameter ( ) = this }
37+ }
38+
39+ private Identifier parameterIdentifier ( Parameter p ) {
40+ result = p or
41+ result = p .( SplatParameter ) .getName ( ) or
42+ result = p .( HashSplatParameter ) .getName ( ) or
43+ result = p .( BlockParameter ) .getName ( ) or
44+ result = p .( OptionalParameter ) .getName ( ) or
45+ result = p .( KeywordParameter ) .getName ( ) or
46+ result = destructuredIdentifier ( p .( DestructuredParameter ) )
47+ }
48+
49+ private Identifier destructuredIdentifier ( AstNode node ) {
50+ result = node or
51+ result = destructuredIdentifier ( node .( DestructuredParameter ) .getAFieldOrChild ( ) )
52+ }
53+
54+ /** Holds if `scope` defines `name` in its parameter declaration. */
1755private predicate scopeDefinesParameter ( VariableScope scope , string name , Location location ) {
18- exists ( Identifier var |
19- name = var .getValue ( ) and
20- location = var .getLocation ( ) and
21- var in [ scope
22- .( BlockScope )
23- .getScopeElement ( )
24- .getAFieldOrChild ( )
25- .( BlockParameters )
26- .getAFieldOrChild + ( ) ,
27- scope
28- .( MethodScope )
29- .getScopeElement ( )
30- .getAFieldOrChild ( )
31- .( MethodParameters )
32- .getAFieldOrChild + ( ) ]
33- )
56+ location =
57+ min ( Parameter p , Identifier i |
58+ scope = p .getDeclaringScope ( ) and
59+ i = parameterIdentifier ( p ) and
60+ name = i .getValue ( )
61+ |
62+ i .getLocation ( ) as loc order by loc .getStartLine ( ) , loc .getStartColumn ( )
63+ )
3464}
3565
3666/** Holds if `var` is assigned in `scope`. */
@@ -77,10 +107,9 @@ private module Cached {
77107
78108 cached
79109 newtype TVariable =
80- TParameter ( VariableScope scope , string name , Location location ) {
81- scopeDefinesParameter ( scope , name , location )
82- } or
83110 TLocalVariable ( VariableScope scope , string name , Location location ) {
111+ scopeDefinesParameter ( scope , name , location )
112+ or
84113 not scopeDefinesParameter ( scope , name , _) and
85114 not blockScopeInherits ( scope , name , _) and
86115 location =
@@ -146,23 +175,6 @@ class Variable extends TVariable {
146175 VariableAccess getAnAccess ( ) { result .getVariable ( ) = this }
147176}
148177
149- /** A parameter. */
150- class Parameter extends Variable {
151- private VariableScope scope ;
152- private string name ;
153- private Location location ;
154-
155- Parameter ( ) { this = TParameter ( scope , name , location ) }
156-
157- final override string getName ( ) { result = name }
158-
159- final override Location getLocation ( ) { result = location }
160-
161- final override VariableScope getDeclaringScope ( ) { result = scope }
162-
163- final override ParameterAccess getAnAccess ( ) { result = super .getAnAccess ( ) }
164- }
165-
166178/** A local variable. */
167179class LocalVariable extends Variable {
168180 private VariableScope scope ;
@@ -176,8 +188,6 @@ class LocalVariable extends Variable {
176188 final override Location getLocation ( ) { result = location }
177189
178190 final override VariableScope getDeclaringScope ( ) { result = scope }
179-
180- final override LocalVariableAccess getAnAccess ( ) { result = super .getAnAccess ( ) }
181191}
182192
183193/** An identifier that refers to a variable. */
@@ -194,16 +204,17 @@ class VariableAccess extends Identifier {
194204
195205/** An identifier that refers to a parameter. */
196206class ParameterAccess extends VariableAccess {
197- override Parameter variable ;
207+ Parameter parameter ;
198208
199- final override Parameter getVariable ( ) { result = variable }
200- }
201-
202- /** An identifier that refers to a local variable. */
203- class LocalVariableAccess extends VariableAccess {
204- override LocalVariable variable ;
209+ ParameterAccess ( ) {
210+ exists ( Identifier i |
211+ i = parameterIdentifier ( parameter ) and
212+ variable .getDeclaringScope ( ) = parameter .getDeclaringScope ( ) and
213+ variable .getLocation ( ) = i .getLocation ( )
214+ )
215+ }
205216
206- final override LocalVariable getVariable ( ) { result = super . getVariable ( ) }
217+ final Parameter getParameter ( ) { result = parameter }
207218}
208219
209220/** A top-level scope. */
0 commit comments