forked from github/codeql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCall.qll
More file actions
924 lines (826 loc) · 23.8 KB
/
Copy pathCall.qll
File metadata and controls
924 lines (826 loc) · 23.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
/**
* Provides all call classes.
*
* All calls have the common base class `Call`.
*/
import Expr
private import semmle.code.csharp.dataflow.internal.DataFlowDispatch
private import semmle.code.csharp.dataflow.internal.DataFlowImplCommon
private import semmle.code.csharp.dispatch.Dispatch
/**
* A call. Either a method call (`MethodCall`), a constructor initializer call
* (`ConstructorInitializer`), a call to a user-defined operator (`OperatorCall`),
* a delegate call (`DelegateCall`), an accessor call (`AccessorCall`), a
* constructor call (`ObjectCreation`), or a local function call (`LocalFunctionCall`).
*/
class Call extends Expr, @call {
/**
* Gets the static (compile-time) target of this call. For example, the
* static target of `x.M()` on line 9 is `A.M` in
*
* ```csharp
* class A {
* virtual void M() { }
* }
*
* class B : A {
* override void M() { }
*
* static void CallM(A x) {
* x.M();
* }
* }
* ```
*
* Use `getARuntimeTarget()` instead to get a potential run-time target (will
* include `B.M` in the example above).
*/
Callable getTarget() { none() }
/** Gets the `i`th argument to this call, if any. */
Expr getArgument(int i) { result = this.getChild(i) and i >= 0 }
/**
* Gets the `i`th "raw" argument to this call, if any.
* For instance methods, argument 0 is the qualifier.
*/
Expr getRawArgument(int i) { result = this.getArgument(i) }
/** Gets an argument to this call. */
Expr getAnArgument() { result = this.getArgument(_) }
/** Gets the number of arguments of this call. */
int getNumberOfArguments() { result = count(this.getAnArgument()) }
/** Holds if this call has no arguments. */
predicate hasNoArguments() { not exists(this.getAnArgument()) }
/**
* Gets the argument for this call associated with the parameter `p`, if any.
*
* This takes into account both positional and named arguments, but does not
* consider default arguments.
*/
cached
Expr getArgumentForParameter(Parameter p) {
// Appears in the positional part of the call
result = this.getImplicitArgument(p)
or
// Appears in the named part of the call
this.getTarget().getAParameter() = p and
result = this.getExplicitArgument(p.getName())
}
pragma[noinline]
private Expr getImplicitArgument(Parameter p) {
this.getTarget().getAParameter() = p and
not exists(result.getExplicitArgumentName()) and
(
p.(Parameter).isParams() and
result = this.getArgument(any(int i | i >= p.getPosition()))
or
not p.(Parameter).isParams() and
result = this.getArgument(p.getPosition())
)
}
pragma[nomagic]
private Expr getExplicitArgument(string name) {
result = this.getAnArgument() and
result.getExplicitArgumentName() = name
}
/**
* Gets the argument to this call associated with the parameter with the given
* `name`, if any.
*
* This takes into account both positional and named arguments, but does not
* consider default arguments.
*/
Expr getArgumentForName(string name) {
exists(Parameter p |
result = this.getArgumentForParameter(p) and
p.hasName(name)
)
}
/**
* Gets a potential run-time target of this call.
*
* A potential target is a callable that is neither abstract nor defined in an
* interface.
*
* Unlike `getTarget()`, this predicate takes reflection/dynamic based calls,
* virtual dispatch, and delegate calls into account. Example:
*
* ```csharp
* class A {
* virtual void M() { }
* }
*
* class B : A {
* override void M() { }
*
* static void CallM(A a) {
* a.M();
* typeof(A).InvokeMember("M", BindingFlags.Public, null, a, new object[0]);
* }
* }
*
* class C {
* void M2(Action<int> a) {
* a(0);
* }
*
* static void CallM2(C c) {
* c.M2(i => { });
* }
* }
* ```
*
* - Line 9: The static target is `A.M()`, whereas the run-time targets are both
* `A.M()` and `B.M()`.
*
* - Line 10: The static target is `Type.InvokeMember()`, whereas the run-time targets
* are both `A.M()` and `B.M()`.
*
* - Line 16: There is no static target (delegate call) but the delegate `i => { }`
* (line 20) is a run-time target.
*/
Callable getARuntimeTarget() {
exists(DispatchCall dc | dc.getCall() = this | result = dc.getADynamicTarget())
}
/**
* Gets the argument that corresponds to the `i`th parameter of a potential
* run-time target of this call.
*
* Unlike `getArgument()`, this predicate takes reflection based calls and named
* arguments into account. Example:
*
* ```csharp
* class A {
* virtual void M(int first, int second) { }
*
* static void CallM(A a) {
* a.M(second: 1, first: 2);
* typeof(A).InvokeMember("M", BindingFlags.Public, null, a, new int[]{ 1, 2 });
* }
* }
* ```
*
* - Line 5: The first and second arguments are `1` and `2`, respectively. However,
* the first and second run-time arguments of `A.M()` are `2` and `1`, respectively.
*
* - Line 6: The static target is `Type.InvokeMember()` and the first, second, third,
* fourth, and fifth arguments are `"M"`, `BindingFlags.Public`, `null`, `a`, and
* `new int[]{ 1, 2 }`, respectively. However, the run-time target is `A.M()` and
* the first and second arguments are `1` and `2`, respectively.
*/
Expr getRuntimeArgument(int i) {
exists(DispatchCall dc | dc.getCall() = this | result = dc.getArgument(i))
}
/**
* Gets the argument that corresponds to parameter `p` of a potential
* run-time target of this call.
*
* This takes into account both positional and named arguments, but does not
* consider default arguments.
*/
cached
Expr getRuntimeArgumentForParameter(Parameter p) {
// Appears in the positional part of the call
result = this.getImplicitRuntimeArgument(p)
or
// Appears in the named part of the call
this.getARuntimeTarget().getAParameter() = p and
result = this.getExplicitRuntimeArgument(p.getName())
}
pragma[noinline]
private Expr getImplicitRuntimeArgument(Parameter p) {
this.getARuntimeTarget().getAParameter() = p and
not exists(result.getExplicitArgumentName()) and
(
p.isParams() and
result = this.getRuntimeArgument(any(int i | i >= p.getPosition()))
or
not p.isParams() and
result = this.getRuntimeArgument(p.getPosition())
)
}
pragma[nomagic]
private Expr getExplicitRuntimeArgument(string name) {
result = this.getARuntimeArgument() and
result.getExplicitArgumentName() = name
}
/**
* Gets the argument that corresponds to a parameter named `name` of a potential
* run-time target of this call.
*/
Expr getRuntimeArgumentForName(string name) {
exists(Parameter p |
result = this.getRuntimeArgumentForParameter(p) and
p.hasName(name)
)
}
/**
* Gets an argument that corresponds to a parameter of a potential
* run-time target of this call.
*/
Expr getARuntimeArgument() { result = this.getRuntimeArgument(_) }
/**
* Gets the number of arguments that correspond to a parameter of a potential
* run-time target of this call.
*/
int getNumberOfRuntimeArguments() { result = count(this.getARuntimeArgument()) }
/**
* Holds if this call has no arguments that correspond to a parameter of a
* potential (run-time) target of this call.
*/
predicate hasNoRuntimeArguments() { not exists(this.getARuntimeArgument()) }
override string toString() { result = "call" }
}
/**
* A method call, for example `a.M()` on line 5 in
*
* ```csharp
* class A {
* void M() { }
*
* static void CallM(A a) {
* a.M();
* }
* }
* ```
*/
class MethodCall extends Call, QualifiableExpr, LateBindableExpr, @method_invocation_expr {
override Method getTarget() { expr_call(this, result) }
/**
* Gets the accessor that was used to generate this method, if any. For example, the
* method call `MyExtensions.get_FirstChar(s)` on line 9 is generated from the property
* accessor `get_FirstChar` on line 3 in
*
* ```csharp
* static class MyExtensions {
* extension(string s) {
* public char FirstChar { get { ... } }
* }
* }
*
* class A {
* char M(string s) {
* return MyExtensions.get_FirstChar(s);
* }
* }
*/
Accessor getTargetAccessor() { expr_call(this, result) }
override Method getQualifiedDeclaration() { result = this.getTarget() }
override string toString() {
if exists(this.getTargetAccessor())
then result = "call to extension accessor " + concat(this.getTargetAccessor().getName())
else result = "call to method " + concat(this.getTarget().getName())
}
override string getAPrimaryQlClass() { result = "MethodCall" }
override Expr getRawArgument(int i) {
if exists(this.getQualifier())
then
i = 0 and result = this.getQualifier()
or
result = this.getArgument(i - 1)
else result = this.getArgument(i)
}
override Expr stripImplicit() {
if this.isImplicit() then result = this.getQualifier().stripImplicit() else result = this
}
}
/**
* A call to an extension method, for example lines 5 and 6 in
*
* ```csharp
* static class A {
* static void M(this int i) { }
*
* static void CallM(int x) {
* x.M();
* M(x);
* }
* }
* ```
*
* Although, syntactically, `x` is a qualifier on line 5, it is treated
* as the first argument in the method call (similar to the call on line
* 6), in order to be properly matched with the parameter `i` on line 2.
*/
class ExtensionMethodCall extends MethodCall {
ExtensionMethodCall() { this.getTarget() instanceof ExtensionMethod }
override TypeAccess getQualifier() { result = this.getChildExpr(-1) }
override Expr getArgument(int i) {
exists(int j | result = this.getChildExpr(j) |
if this.isOrdinaryStaticCall() then (j = i and j >= 0) else (j = i - 1 and j >= -1)
)
}
/**
* Holds if this call is an ordinary static method call, where the target
* happens to be an extension method, for example the calls on lines 6 and
* 7 (but not line 5) in
*
* ```csharp
* static class Extensions {
* public static void Ext(int i) { }
*
* static void M(int i) {
* i.Ext();
* Ext(i);
* Extensions.Ext(i);
* }
* }
* ```
*/
predicate isOrdinaryStaticCall() {
not exists(this.getChildExpr(-1)) // `Ext(i)` case above
or
exists(this.getQualifier()) // `Extensions.Ext(i)` case above
}
}
/**
* A virtual method call, for example `a.M()` on line 5 in
*
* ```csharp
* class A {
* public virtual void M() { }
*
* static void CallM(A a) {
* a.M();
* }
* }
* ```
*/
class VirtualMethodCall extends MethodCall {
VirtualMethodCall() {
not this.getQualifier() instanceof BaseAccess and
this.getTarget().isOverridableOrImplementable()
}
}
/**
* A constructor initializer call, for example `base()` (line 6) and
* `this(0)` (line 8) in
*
* ```csharp
* class A
* {
* public A() { }
* }
*
* class B : A
* {
* public B(int x) : base() { }
*
* public B() : this(0) { }
* }
* ```
*/
class ConstructorInitializer extends Call, @constructor_init_expr {
override Constructor getTarget() { expr_call(this, result) }
override Constructor getARuntimeTarget() { result = Call.super.getARuntimeTarget() }
override string toString() { result = "call to constructor " + this.getTarget().getName() }
override string getAPrimaryQlClass() { result = "ConstructorInitializer" }
private ValueOrRefType getTargetType() {
result = this.getTarget().getDeclaringType().getUnboundDeclaration()
}
private ValueOrRefType getConstructorType() {
result = this.getConstructor().getDeclaringType().getUnboundDeclaration()
}
/**
* Holds if this initializer is a `this` initializer, for example `this(0)`
* in
*
* ```csharp
* class A
* {
* A(int i) { }
* A() : this(0) { }
* }
* ```
*/
predicate isThis() { this.getTargetType() = this.getConstructorType() }
/**
* Holds if this initializer is a `base` initializer, for example `base(0)`
* in
*
* ```csharp
* class A
* {
* A(int i) { }
* }
*
* class B : A
* {
* B() : base(0) { }
* }
* ```
*/
predicate isBase() { this.getTargetType() != this.getConstructorType() }
/**
* Gets the constructor that this initializer call belongs to. For example,
* the initializer call `base()` on line 7 belongs to the constructor `B`
* on line 6 in
*
* ```csharp
* class A
* {
* public A() { }
* }
*
* class B : A
* {
* public B() : base() { }
* }
* ```
*/
Constructor getConstructor() { result.getInitializer() = this }
override Expr getRawArgument(int i) {
if this.getTarget().isStatic()
then result = this.getArgument(i)
else result = this.getArgument(i - 1)
}
}
/**
* A call to an operator, for example `this + other`
* on line 7 in
*
* ```csharp
* class A {
* public static A operator+(A left, A right) {
* return left;
* }
*
* public A Add(A other) {
* return this + other;
* }
* }
* ```
*/
class OperatorCall extends Call, LateBindableExpr, @op_invoke_expr {
override Operator getTarget() { expr_call(this, result) }
override Operator getARuntimeTarget() { result = Call.super.getARuntimeTarget() }
override string toString() {
if this instanceof DynamicOperatorCall
then result = "dynamic call to operator " + this.getLateBoundTargetName()
else result = "call to operator " + this.getTarget().getName()
}
override string getAPrimaryQlClass() { result = "OperatorCall" }
}
/**
* A call to an extension operator, for example `3 * s` on
* line 9 in
*
* ```csharp
* static class MyExtensions {
* extension(string s) {
* public static string operator *(int i, string s) { ... }
* }
* }
*
* class A {
* string M(string s) {
* return 3 * s;
* }
* }
* ```
*/
class ExtensionOperatorCall extends OperatorCall {
ExtensionOperatorCall() { this.getTarget() instanceof ExtensionOperator }
override string getAPrimaryQlClass() { result = "ExtensionOperatorCall" }
private predicate isOrdinaryStaticCall() {
not exists(Expr e | e = this.getChildExpr(-1) | not e instanceof TypeAccess)
}
override Expr getArgument(int i) {
exists(int j | result = this.getChildExpr(j) |
i >= 0 and
if this.isOrdinaryStaticCall() or this.getTarget() instanceof CompoundAssignmentOperator
then j = i
else j = i - 1
)
}
}
/**
* A call to a user-defined mutator operator, for example `a++` on
* line 7 in
*
* ```csharp
* class A {
* public static A operator++(A a) {
* return a;
* }
*
* public static A Increment(A a) {
* return a++;
* }
* }
* ```
*/
class MutatorOperatorCall extends OperatorCall {
MutatorOperatorCall() { mutator_invocation_mode(this, _) }
/** Holds if the operator is in prefix position. */
predicate isPrefix() { mutator_invocation_mode(this, 1) }
/** Holds if the operator is in postfix position. */
predicate isPostfix() { mutator_invocation_mode(this, 2) }
}
/**
* A call to an instance mutator operator, for example `a++` on
* line 5 in
*
* ```csharp
* class A {
* public void operator ++() { ... }
*
* public static void Increment(A a) {
* a++;
* }
* }
* ```
*/
class InstanceMutatorOperatorCall extends MutatorOperatorCall {
InstanceMutatorOperatorCall() { this.getTarget().getNumberOfParameters() = 0 }
/** Gets the qualifier of this instance mutator operator call. */
Expr getQualifier() { result = this.getChildExpr(0) }
override Expr getArgument(int i) { none() }
}
/**
* A call to a compound assignment operator, for example `this += other`
* on line 7 in
*
* ```csharp
* class A {
* public void operator +=(A other) {
* ...
* }
*
* public void Add(A other) {
* this += other;
* }
* }
* ```
*/
class CompoundAssignmentOperatorCall extends AssignCallOperation {
CompoundAssignmentOperatorCall() { this.getTarget() instanceof CompoundAssignmentOperator }
override Expr getArgument(int i) { result = this.getChildExpr(i + 1) and i >= 0 }
/** Gets the qualifier of this compound assignment operator call. */
override Expr getQualifier() { result = this.getChildExpr(0) }
}
/**
* A call to a compound assignment extension operator, for example `s1 *= s2` on
* line 9 in
*
* ```csharp
* static class MyExtensions {
* extension(string s) {
* public void operator *=(string other) { ... }
* }
* }
*
* class A {
* void M(string s1, string s2) {
* s1 *= s2;
* }
* }
*/
class ExtensionCompoundAssignmentOperatorCall extends CompoundAssignmentOperatorCall,
ExtensionOperatorCall
{
override Expr getArgument(int i) { result = ExtensionOperatorCall.super.getArgument(i) }
override Expr getQualifier() { none() }
override string getAPrimaryQlClass() { result = "ExtensionCompoundAssignmentOperatorCall" }
}
private class DelegateLikeCall_ = @delegate_invocation_expr or @function_pointer_invocation_expr;
/**
* A function pointer or delegate call.
*/
class DelegateLikeCall extends Call, DelegateLikeCall_ {
override Callable getTarget() { none() }
/**
* Gets the delegate or function pointer expression of this call. For example, the
* delegate expression of `X()` on line 5 is the access to the field `X` in
*
* ```csharp
* class A {
* Action X = () => { };
*
* void CallX() {
* X();
* }
* }
* ```
*/
Expr getExpr() { result = this.getChild(-1) }
final override Callable getARuntimeTarget() {
exists(ExplicitDelegateLikeDataFlowCall call |
this = call.getCall() and
result = viableCallableLambda(call, _).asCallable(_)
)
}
override Expr getRuntimeArgument(int i) { result = this.getArgument(i) }
}
/**
* A delegate call, for example `x()` on line 5 in
*
* ```csharp
* class A {
* Action X = () => { };
*
* void CallX() {
* X();
* }
* }
* ```
*/
class DelegateCall extends DelegateLikeCall, @delegate_invocation_expr {
override string toString() { result = "delegate call" }
override string getAPrimaryQlClass() { result = "DelegateCall" }
}
/**
* A function pointer call, for example `fp(1)` on line 3 in
*
* ```csharp
* class A {
* void Call(delegate*<int, void> fp) {
* fp(1);
* }
* }
* ```
*/
class FunctionPointerCall extends DelegateLikeCall, @function_pointer_invocation_expr {
override string toString() { result = "function pointer call" }
override string getAPrimaryQlClass() { result = "FunctionPointerCall" }
}
/**
* A call to an accessor. Either a property accessor call (`PropertyCall`),
* an indexer accessor call (`IndexerCall`), or an event accessor call
* (`EventCall`).
*/
class AccessorCall extends Call, QualifiableExpr, @call_access_expr {
override Accessor getTarget() { result = this.getReadTarget() or result = this.getWriteTarget() }
/**
* Gets the static (compile-time) target of this call, assuming that this is
* an `AssignableRead`.
*
* Note that left-hand sides of compound assignments are both
* `AssignableRead`s and `AssignableWrite`s.
*/
Accessor getReadTarget() { none() }
/**
* Gets the static (compile-time) target of this call, assuming that this is
* an `AssignableWrite`.
*
* Note that left-hand sides of compound assignments are both
* `AssignableRead`s and `AssignableWrite`s.
*/
Accessor getWriteTarget() { none() }
override Expr getArgument(int i) { none() }
override Accessor getARuntimeTarget() { result = Call.super.getARuntimeTarget() }
}
/**
* A call to a property accessor, for example the call to `get_P` on
* line 5 in
*
* ```csharp
* class A {
* int P { get { return 0; } }
*
* public int GetP() {
* return P;
* }
* }
* ```
*/
class PropertyCall extends AccessorCall, PropertyAccessExpr {
override Accessor getReadTarget() {
this instanceof AssignableRead and result = this.getProperty().getReadTarget()
}
override Accessor getWriteTarget() {
this instanceof AssignableWrite and
result = this.getProperty().getWriteTarget()
}
override Expr getArgument(int i) {
i = 0 and
result = AssignableInternal::getAccessorCallValueArgument(this)
}
override string toString() { result = PropertyAccessExpr.super.toString() }
override string getAPrimaryQlClass() { result = "PropertyCall" }
}
/**
* A call to an indexer accessor, for example the call to `get_Item`
* (defined on line 3) on line 7 in
*
* ```csharp
* class A {
* string this[int i] {
* get { return i.ToString(); }
* }
*
* public string GetItem(int i) {
* return this[i];
* }
* }
* ```
*/
class IndexerCall extends AccessorCall, IndexerAccessExpr {
override Accessor getReadTarget() {
this instanceof AssignableRead and result = this.getIndexer().getReadTarget()
}
override Accessor getWriteTarget() {
this instanceof AssignableWrite and
result = this.getIndexer().getWriteTarget()
}
override Expr getArgument(int i) {
result = this.(ElementAccess).getIndex(i)
or
i = count(this.(ElementAccess).getAnIndex()) and
result = AssignableInternal::getAccessorCallValueArgument(this)
}
override string toString() { result = IndexerAccessExpr.super.toString() }
override string getAPrimaryQlClass() { result = "IndexerCall" }
}
/**
* A call to an extension property accessor (via the property), for example
* `s.FirstChar` on line 9 in
*
* ```csharp
* static class MyExtensions {
* extension(string s) {
* public char FirstChar { get { ... } }
* }
* }
*
* class A {
* char M(string s) {
* return s.FirstChar;
* }
* }
* ```
*/
class ExtensionPropertyCall extends PropertyCall {
private ExtensionProperty prop;
ExtensionPropertyCall() { this.getProperty() = prop }
override Expr getArgument(int i) {
if prop.isStatic()
then result = super.getArgument(i)
else (
// Shift arguments as the qualifier is an explicit argument in the getter/setter.
i = 0 and
result = this.getQualifier()
or
result = super.getArgument(i - 1)
)
}
override string getAPrimaryQlClass() { result = "ExtensionPropertyCall" }
}
/**
* A call to an event accessor, for example the call to `add_Click`
* (defined on line 5) on line 12 in
*
* ```csharp
* class A {
* public delegate void EventHandler(object sender, object e);
*
* public event EventHandler Click {
* add { ... }
* remove { ... }
* }
*
* void A_Click(object sender, object e) { }
*
* void AddEvent() {
* Click += A_Click;
* }
* }
* ```
*/
class EventCall extends AccessorCall, EventAccessExpr {
override EventAccessor getWriteTarget() {
exists(Event e, AddOrRemoveEventExpr aoree |
e = this.getEvent() and
aoree.getLeftOperand() = this
|
aoree instanceof AddEventExpr and result = e.getAddEventAccessor()
or
aoree instanceof RemoveEventExpr and result = e.getRemoveEventAccessor()
)
}
override Expr getArgument(int i) {
i = 0 and
exists(AddOrRemoveEventExpr aoree |
aoree.getLeftOperand() = this and
result = aoree.getRightOperand()
)
}
override string toString() { result = EventAccessExpr.super.toString() }
override string getAPrimaryQlClass() { result = "EventCall" }
}
/**
* A call to a local function, for example the call `Fac(n)` on line 6 in
*
* ```csharp
* int Choose(int n, int m) {
* int Fac(int x) {
* return x > 1 ? x * Fac(x - 1) : 1;
* }
*
* return Fac(n) / (Fac(m) * Fac(n - m));
* }
* ```
*/
class LocalFunctionCall extends Call, @local_function_invocation_expr {
override LocalFunction getTarget() { expr_call(this, result) }
override string toString() { result = "call to local function " + this.getTarget().getName() }
override string getAPrimaryQlClass() { result = "LocalFunctionCall" }
}