Skip to content

Commit eb5cb2a

Browse files
committed
C#: Copy dotnet.Expr implementation.
1 parent 215808d commit eb5cb2a

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

csharp/ql/lib/semmle/code/csharp/exprs/Call.qll

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@ import Expr
88
private import semmle.code.csharp.dataflow.internal.DataFlowDispatch
99
private import semmle.code.csharp.dataflow.internal.DataFlowImplCommon
1010
private import semmle.code.csharp.dispatch.Dispatch
11-
private import dotnet
1211

1312
/**
1413
* A call. Either a method call (`MethodCall`), a constructor initializer call
1514
* (`ConstructorInitializer`), a call to a user-defined operator (`OperatorCall`),
1615
* a delegate call (`DelegateCall`), an accessor call (`AccessorCall`), a
1716
* constructor call (`ObjectCreation`), or a local function call (`LocalFunctionCall`).
1817
*/
19-
class Call extends DotNet::Call, Expr, @call {
18+
class Call extends Expr, @call {
2019
/**
2120
* Gets the static (compile-time) target of this call. For example, the
2221
* static target of `x.M()` on line 9 is `A.M` in
@@ -38,13 +37,19 @@ class Call extends DotNet::Call, Expr, @call {
3837
* Use `getARuntimeTarget()` instead to get a potential run-time target (will
3938
* include `B.M` in the example above).
4039
*/
41-
override Callable getTarget() { none() }
40+
Callable getTarget() { none() }
4241

43-
override Expr getArgument(int i) { result = this.getChild(i) and i >= 0 }
42+
/** Gets the `i`th argument to this call, if any. */
43+
Expr getArgument(int i) { result = this.getChild(i) and i >= 0 }
4444

45-
override Expr getRawArgument(int i) { result = this.getArgument(i) }
45+
/**
46+
* Gets the `i`th "raw" argument to this call, if any.
47+
* For instance methods, argument 0 is the qualifier.
48+
*/
49+
Expr getRawArgument(int i) { result = this.getArgument(i) }
4650

47-
override Expr getAnArgument() { result = this.getArgument(_) }
51+
/** Gets an argument to this call. */
52+
Expr getAnArgument() { result = this.getArgument(_) }
4853

4954
/** Gets the number of arguments of this call. */
5055
int getNumberOfArguments() { result = count(this.getAnArgument()) }
@@ -59,7 +64,7 @@ class Call extends DotNet::Call, Expr, @call {
5964
* consider default arguments.
6065
*/
6166
cached
62-
override Expr getArgumentForParameter(DotNet::Parameter p) {
67+
Expr getArgumentForParameter(DotNet::Parameter p) {
6368
// Appears in the positional part of the call
6469
result = this.getImplicitArgument(p)
6570
or
@@ -144,7 +149,7 @@ class Call extends DotNet::Call, Expr, @call {
144149
* - Line 16: There is no static target (delegate call) but the delegate `i => { }`
145150
* (line 20) is a run-time target.
146151
*/
147-
override Callable getARuntimeTarget() {
152+
Callable getARuntimeTarget() {
148153
exists(DispatchCall dc | dc.getCall() = this | result = dc.getADynamicTarget())
149154
}
150155

csharp/ql/lib/semmle/code/csharp/exprs/Expr.qll

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ private import semmle.code.csharp.TypeRef
3838
* interpolated string (`InterpolatedStringExpr`), a qualifiable expression
3939
* (`QualifiableExpr`), or a literal (`Literal`).
4040
*/
41-
class Expr extends DotNet::Expr, ControlFlowElement, @expr {
41+
class Expr extends ControlFlowElement, @expr {
4242
override Location getALocation() { expr_location(this, result) }
4343

4444
/** Gets the type of this expression. */
45-
override Type getType() {
45+
Type getType() {
4646
expressions(this, _, result)
4747
or
4848
not expressions(this, _, any(Type t)) and
@@ -53,7 +53,10 @@ class Expr extends DotNet::Expr, ControlFlowElement, @expr {
5353
final AnnotatedType getAnnotatedType() { result.appliesTo(this) }
5454

5555
/** Gets the value of this expression, if any */
56-
override string getValue() { expr_value(this, result) }
56+
string getValue() { expr_value(this, result) }
57+
58+
/** Holds if this expression has a value. */
59+
final predicate hasValue() { exists(this.getValue()) }
5760

5861
/** Gets the enclosing statement of this expression, if any. */
5962
final Stmt getEnclosingStmt() { enclosingStmt(this, result) }
@@ -88,6 +91,10 @@ class Expr extends DotNet::Expr, ControlFlowElement, @expr {
8891
*/
8992
string getExplicitArgumentName() { expr_argument_name(this, result) }
9093

94+
/**
95+
* Gets the parent of this expression. This is for example the element
96+
* that uses the result of this expression.
97+
*/
9198
override Element getParent() { result = ControlFlowElement.super.getParent() }
9299

93100
/** Holds if the nullable flow state of this expression is not null. */

0 commit comments

Comments
 (0)