Skip to content

Commit c1e627d

Browse files
authored
Merge pull request #1312 from hvitved/csharp/variable-preds
C#: Refactor predicates in `Variable` class to be defined by dispatch
2 parents d1a711e + e3b83d0 commit c1e627d

File tree

1 file changed

+32
-46
lines changed

1 file changed

+32
-46
lines changed

csharp/ql/src/semmle/code/csharp/Variable.qll

Lines changed: 32 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -14,54 +14,12 @@ private import semmle.code.csharp.ExprOrStmtParent
1414
* A variable. Either a variable with local scope (`LocalScopeVariable`) or a field (`Field`).
1515
*/
1616
class Variable extends Assignable, DotNet::Variable, @variable {
17-
/** Gets the type that declares this variable, if any. */
18-
override ValueOrRefType getDeclaringType() {
19-
fields(this, _, _, result, _, _)
20-
or
21-
exists(Expr e |
22-
localvars(this, _, _, _, _, e) and
23-
result = e.getEnclosingStmt().getEnclosingCallable().getDeclaringType()
24-
)
25-
or
26-
exists(Callable parent |
27-
params(this, _, _, _, _, parent, _) and result = parent.getDeclaringType()
28-
)
29-
or
30-
exists(Indexer indexer |
31-
params(this, _, _, _, _, indexer, _) and result = indexer.getDeclaringType()
32-
)
33-
or
34-
exists(@delegate_type t | params(this, _, _, _, _, t, _) and result = t)
35-
or
36-
exists(Expr parent |
37-
params(this, _, _, _, _, parent, _) and
38-
result = parent.getEnclosingCallable().getDeclaringType()
39-
)
40-
}
41-
4217
override Variable getSourceDeclaration() { result = this }
4318

44-
override string getName() {
45-
params(this, result, _, _, _, _, _) or
46-
localvars(this, _, result, _, _, _) or
47-
fields(this, _, result, _, _, _)
48-
}
49-
50-
/** Gets the type of this variable. For example `int` in `int x`. */
51-
override Type getType() {
52-
params(this, _, getTypeRef(result), _, _, _, _) or
53-
localvars(this, _, _, _, getTypeRef(result), _) or
54-
fields(this, _, _, _, getTypeRef(result), _)
55-
}
56-
57-
override Location getALocation() {
58-
param_location(this, result) or
59-
localvar_location(this, result) or
60-
field_location(this, result)
61-
}
62-
6319
override VariableAccess getAnAccess() { result.getTarget() = this }
6420

21+
override Type getType() { none() }
22+
6523
/** Gets the expression used to initialise this variable, if any. */
6624
Expr getInitializer() { none() }
6725
}
@@ -222,6 +180,18 @@ class Parameter extends DotNet::Parameter, LocalScopeVariable, Attributable, Top
222180

223181
override Parameter getSourceDeclaration() { params(this, _, _, _, _, _, result) }
224182

183+
override ValueOrRefType getDeclaringType() {
184+
exists(Parameterizable p | p = this.getDeclaringElement() |
185+
if p instanceof DelegateType then result = p else result = p.getDeclaringType()
186+
)
187+
}
188+
189+
override string getName() { params(this, result, _, _, _, _, _) }
190+
191+
override Type getType() { params(this, _, getTypeRef(result), _, _, _, _) }
192+
193+
override Location getALocation() { param_location(this, result) }
194+
225195
override string toString() { result = this.getName() }
226196

227197
/**
@@ -272,8 +242,6 @@ class Parameter extends DotNet::Parameter, LocalScopeVariable, Attributable, Top
272242
// At least one other definition than the implicit entry definition
273243
strictcount(AssignableDefinition def | def.getTarget() = this) > 1
274244
}
275-
276-
override Type getType() { result = LocalScopeVariable.super.getType() }
277245
}
278246

279247
/**
@@ -354,6 +322,16 @@ class LocalVariable extends LocalScopeVariable, @local_variable {
354322
override Callable getCallable() { result = getEnclosingCallable() }
355323

356324
override predicate isRef() { localvars(this, 3, _, _, _, _) }
325+
326+
override ValueOrRefType getDeclaringType() {
327+
result = this.getVariableDeclExpr().getEnclosingCallable().getDeclaringType()
328+
}
329+
330+
override string getName() { localvars(this, _, result, _, _, _) }
331+
332+
override Type getType() { localvars(this, _, _, _, getTypeRef(result), _) }
333+
334+
override Location getALocation() { localvar_location(this, result) }
357335
}
358336

359337
/**
@@ -399,6 +377,14 @@ class Field extends Variable, AssignableMember, Attributable, TopLevelExprParent
399377

400378
override FieldAccess getAnAccess() { result = Variable.super.getAnAccess() }
401379

380+
override ValueOrRefType getDeclaringType() { fields(this, _, _, result, _, _) }
381+
382+
override string getName() { fields(this, _, result, _, _, _) }
383+
384+
override Type getType() { fields(this, _, _, _, getTypeRef(result), _) }
385+
386+
override Location getALocation() { field_location(this, result) }
387+
402388
override string toString() { result = Variable.super.toString() }
403389
}
404390

0 commit comments

Comments
 (0)