-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathMember.qll
523 lines (455 loc) · 17.5 KB
/
Member.qll
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
/** Provides classes relating to declarations and type members. */
import Callable
import Element
import Modifier
import Variable
private import Implements
private import TypeRef
private import commons.QualifiedName
private module QualifiedNameInput implements QualifiedNameInputSig {
string getUnboundGenericSuffix(UnboundGeneric ug) {
result = "<" + strictconcat(int i | exists(ug.getTypeParameter(i)) | "", ",") + ">"
}
}
private module FullyQualifiedNameInput implements QualifiedNameInputSig {
string getUnboundGenericSuffix(UnboundGeneric ug) {
result = "`" + ug.getNumberOfTypeParameters()
}
}
/**
* A declaration.
*
* Either a modifiable (`Modifiable`) or an assignable (`Assignable`).
*/
class Declaration extends NamedElement, @declaration {
/** Gets the name of this declaration, without additional decoration such as `<...>`. */
string getUndecoratedName() { none() }
/** Holds if this element has undecorated name 'name'. */
final predicate hasUndecoratedName(string name) { name = this.getUndecoratedName() }
/**
* Gets the unbound version of this declaration, that is, the declaration where
* all type arguments have been removed. For example, in
*
* ```csharp
* class C<T>
* {
* class Nested
* {
* }
*
* void Method<S>() { }
* }
* ```
*
* we have the following
*
* | Declaration | Unbound declaration |
* |-------------------------|---------------------|
* | `C<int>` | ``C`1`` |
* | ``C`1.Nested`` | ``C`1.Nested`` |
* | `C<int>.Nested` | ``C`1.Nested`` |
* | ``C`1.Method`1`` | ``C`1.Method`1`` |
* | ``C<int>.Method`1`` | ``C`1.Method`1`` |
* | `C<int>.Method<string>` | ``C`1.Method`1`` |
*/
Declaration getUnboundDeclaration() { result = this }
/** Holds if this declaration is unbound. */
final predicate isUnboundDeclaration() { this.getUnboundDeclaration() = this }
/** Gets the type containing this declaration, if any. */
ValueOrRefType getDeclaringType() { none() }
/** Holds if this declaration is unconstructed and in source code. */
final predicate isSourceDeclaration() { this.fromSource() and this.isUnboundDeclaration() }
override string toString() { result = this.getName() }
override predicate hasFullyQualifiedName(string qualifier, string name) {
QualifiedName<FullyQualifiedNameInput>::hasQualifiedName(this, qualifier, name)
}
/**
* Gets the fully qualified name of this declaration, including types, for example
* the fully qualified name with types of `M` on line 3 is `N.C.M(int, string)` in
*
* ```csharp
* namespace N {
* class C {
* void M(int i, string s) { }
* }
* }
* ```
*/
deprecated string getFullyQualifiedNameWithTypes() {
exists(string fullqual, string qual, string name |
this.getDeclaringType().hasFullyQualifiedName(qual, name) and
fullqual = getQualifiedName(qual, name) and
if this instanceof NestedType
then result = fullqual + "+" + this.toStringWithTypes()
else result = fullqual + "." + this.toStringWithTypes()
)
}
/**
* Holds if this declaration has been generated by the compiler, for example
* implicit constructors or accessors.
*/
predicate isCompilerGenerated() { compiler_generated(this) }
}
/** A declaration that can have a modifier. */
class Modifiable extends Declaration, @modifiable {
/** Gets a modifier of this declaration. */
Modifier getAModifier() { has_modifiers(this, result) }
/** Holds if this declaration has `name` as a modifier. */
predicate hasModifier(string name) { this.getAModifier().hasName(name) }
/** Holds if this declaration is `static`. */
predicate isStatic() { this.hasModifier("static") }
/** Holds if this declaration is `public`. */
predicate isPublic() { this.hasModifier("public") }
/** Holds if this declaration is `protected`. */
predicate isProtected() { this.hasModifier("protected") }
/** Holds if this declaration is `internal`. */
predicate isInternal() { this.hasModifier("internal") }
/** Holds if this declaration is `private`. */
predicate isPrivate() { this.hasModifier("private") }
/** Holds if this declaration has the modifier `new`. */
predicate isNew() { this.hasModifier("new") }
/** Holds if this declaration is `sealed`. */
predicate isSealed() { this.hasModifier("sealed") }
/** Holds if this declaration is `abstract`. */
predicate isAbstract() { this.hasModifier("abstract") }
/** Holds if this declaration is `extern`. */
predicate isExtern() { this.hasModifier("extern") }
/** Holds if this declaration is `partial`. */
predicate isPartial() { this.hasModifier("partial") }
/** Holds if this declaration is `const`. */
predicate isConst() { this.hasModifier("const") }
/** Holds if this declaration has the modifier `required`. */
predicate isRequired() { this.hasModifier("required") }
/** Holds if this declaration is `file` local. */
predicate isFile() { this.hasModifier("file") }
/** Holds if this declaration is `unsafe`. */
predicate isUnsafe() {
this.hasModifier("unsafe")
or
exists(Type t, Type child |
t = this.(Parameterizable).getAParameter().getType() or
t = this.(Property).getType() or
t = this.(Callable).getReturnType() or
t = this.(DelegateType).getReturnType()
|
child = t.getAChild*() and
(
child instanceof PointerType
or
child instanceof FunctionPointerType
)
)
}
/** Holds if this declaration is `async`. */
predicate isAsync() { this.hasModifier("async") }
private predicate isReallyPrivate() {
this.isPrivate() and
not this.isProtected() and
// Rare case when a member is defined with the same name in multiple assemblies with different visibility
not this.isPublic()
}
/**
* Holds if this declaration is effectively `private`. A declaration is considered
* effectively `private` if it can only be referenced from
* - the declaring and its nested types, similarly to `private` declarations, and
* - the enclosing types.
*
* Note that explicit interface implementations are also considered effectively
* `private` if the implemented interface is itself effectively `private`. Finally,
* `private protected` members are not considered effectively `private`, because
* they can be overridden within the declaring assembly.
*/
predicate isEffectivelyPrivate() {
this.isReallyPrivate() or
this.getDeclaringType+().(Modifiable).isReallyPrivate() or
this.(Virtualizable).getExplicitlyImplementedInterface().isEffectivelyPrivate()
}
private predicate isReallyInternal() {
(
this.isInternal() and not this.isProtected()
or
this.isPrivate() and this.isProtected()
) and
// Rare case when a member is defined with the same name in multiple assemblies with different visibility
not this.isPublic()
}
/**
* Holds if this declaration is effectively `internal`. A declaration is considered
* effectively `internal` if it can only be referenced from the declaring assembly.
*
* Note that friend assemblies declared in `InternalsVisibleToAttribute` are not
* considered. Explicit interface implementations are also considered effectively
* `internal` if the implemented interface is itself effectively `internal`. Finally,
* `internal protected` members are not considered effectively `internal`, because
* they can be overridden outside the declaring assembly.
*/
predicate isEffectivelyInternal() {
this.isReallyInternal() or
this.getDeclaringType+().(Modifiable).isReallyInternal() or
this.(Virtualizable).getExplicitlyImplementedInterface().isEffectivelyInternal()
}
/**
* Holds if this declaration is effectively `public`, meaning that it can be
* referenced outside the declaring assembly.
*/
predicate isEffectivelyPublic() {
not this.isEffectivelyPrivate() and not this.isEffectivelyInternal()
}
}
/** A declaration that is a member of a type. */
class Member extends Modifiable, @member {
/** Gets an access to this member. */
MemberAccess getAnAccess() { result.getTarget() = this }
/**
* Holds if this member has name `name` and is defined in type `type`
* with namespace `namespace`.
*/
cached
final predicate hasFullyQualifiedName(string namespace, string type, string name) {
QualifiedName<FullyQualifiedNameInput>::hasQualifiedName(this, namespace, type, name)
}
}
private class TOverridable = @virtualizable or @callable_accessor;
/**
* A declaration that can be overridden or implemented. That is, a method,
* a property, an indexer, an event, an accessor, or an operator.
*
* Unlike `Virtualizable`, this class includes accessors.
*/
class Overridable extends Declaration, TOverridable {
/**
* Gets any interface this member explicitly implements; this only applies
* to members that can be declared on an interface, i.e. methods, properties,
* indexers and events.
*/
Interface getExplicitlyImplementedInterface() {
explicitly_implements(this, result)
or
not explicitly_implements(this, any(Interface i)) and
explicitly_implements(this, getTypeRef(result))
}
/**
* Holds if this member implements an interface member explicitly.
*/
predicate implementsExplicitInterface() { exists(this.getExplicitlyImplementedInterface()) }
/** Holds if this member can be overridden or implemented. */
predicate isOverridableOrImplementable() { none() }
/** Gets the member that is immediately overridden by this member, if any. */
Overridable getOverridee() {
overrides(this, result)
or
// For accessors (which are `Callable`s), the extractor generates entries
// in the `overrides` relation. However, we want the relation to be on
// the declarations containing the accessors instead
exists(Accessor accessorOverrider, Accessor accessorOverridee |
accessorOverrider = this.(DeclarationWithAccessors).getAnAccessor() and
accessorOverridee = result.(DeclarationWithAccessors).getAnAccessor() and
overrides(accessorOverrider, accessorOverridee)
)
}
/** Gets a member that immediately overrides this member, if any. */
Overridable getAnOverrider() { this = result.getOverridee() }
/** Holds if this member is overridden by some other member. */
predicate isOverridden() { exists(this.getAnOverrider()) }
/** Holds if this member overrides another member. */
predicate overrides() { exists(this.getOverridee()) }
/**
* Gets the interface member that is immediately implemented by this member, if any.
*
* The type `t` is a type that implements the interface type in which
* the result is declared, in such a way that this member is the
* implementation of the result.
*
* Example:
*
* ```csharp
* interface I { void M(); }
*
* class A { public void M() { } }
*
* class B : A, I { }
*
* class C : A, I { new public void M() }
* ```
*
* In the example above, the following (and nothing else) holds:
* `A.M.getImplementee(B) = I.M` and
* `C.M.getImplementee(C) = I.M`.
*/
Overridable getImplementee(ValueOrRefType t) { implements(this, result, t) }
/** Gets the interface member that is immediately implemented by this member, if any. */
Overridable getImplementee() { result = this.getImplementee(_) }
/**
* Gets a member that immediately implements this interface member, if any.
*
* The type `t` is a type that implements the interface type in which
* this member is declared, in such a way that the result is the
* implementation of this member.
*
* Example:
*
* ```csharp
* interface I { void M(); }
*
* class A { public void M() { } }
*
* class B : A, I { }
*
* class C : A, I { new public void M() }
* ```
*
* In the example above, the following (and nothing else) holds:
* `I.M.getAnImplementor(B) = A.M` and
* `I.M.getAnImplementor(C) = C.M`.
*/
Overridable getAnImplementor(ValueOrRefType t) { this = result.getImplementee(t) }
/** Gets a member that immediately implements this interface member, if any. */
Overridable getAnImplementor() { this = result.getImplementee() }
/**
* Gets an interface member that is (transitively) implemented by this
* member, if any. That is, either this member immediately implements
* the interface member, or this member overrides (transitively) another
* member that immediately implements the interface member.
*
* Note that this is generally *not* equivalent with
* `getOverridee*().getImplementee()`, as the example below illustrates:
*
* ```csharp
* interface I { void M(); }
*
* class A { public virtual void M() { } }
*
* class B : A, I { }
*
* class C : A { public override void M() }
*
* class D : B { public override void M() }
* ```
*
* - If this member is `A.M` then `I.M = getAnUltimateImplementee()`.
* - If this member is `C.M` then it is *not* the case that
* `I.M = getAnUltimateImplementee()`, because `C` is not a sub type of `I`.
* (An example where `getOverridee*().getImplementee()` would be incorrect.)
* - If this member is `D.M` then `I.M = getAnUltimateImplementee()`.
*/
pragma[nomagic]
Overridable getAnUltimateImplementee() {
exists(Overridable implementation, ValueOrRefType implementationType |
implements(implementation, result, implementationType)
|
this = implementation
or
this.getOverridee+() = implementation and
this.getDeclaringType().getABaseType+() = implementationType
)
}
/**
* Gets a member that (transitively) implements this interface member,
* if any. That is, either this interface member is immediately implemented
* by the result, or the result overrides (transitively) another member that
* immediately implements this interface member.
*
* Note that this is generally *not* equivalent with
* `getImplementor().getAnOverrider*()` (see `getImplementee`).
*/
Overridable getAnUltimateImplementor() { this = result.getAnUltimateImplementee() }
/** Holds if this interface member is implemented by some other member. */
predicate isImplemented() { exists(this.getAnImplementor()) }
/** Holds if this member implements (transitively) an interface member. */
predicate implements() { exists(this.getAnUltimateImplementee()) }
/**
* Holds if this member overrides or implements (transitively)
* `that` member.
*/
predicate overridesOrImplements(Overridable that) {
this.getOverridee+() = that or
this.getAnUltimateImplementee() = that
}
/**
* Holds if this member overrides or implements (reflexively, transitively)
* `that` member.
*/
predicate overridesOrImplementsOrEquals(Overridable that) {
this = that or
this.overridesOrImplements(that)
}
}
/**
* A member where the `virtual` modifier is valid. That is, a method,
* a property, an indexer, an event, or an operator.
*
* Equivalently, these are the members that can be defined in an interface.
*
* Unlike `Overridable`, this class excludes accessors.
*/
class Virtualizable extends Overridable, Member, @virtualizable {
/** Holds if this member has the modifier `override`. */
predicate isOverride() { this.hasModifier("override") }
/** Holds if this member is `virtual`. */
predicate isVirtual() { this.hasModifier("virtual") }
override predicate isPublic() {
Member.super.isPublic() or
this.implementsExplicitInterface()
}
override predicate isPrivate() {
super.isPrivate() and
not this.implementsExplicitInterface()
}
override predicate isOverridableOrImplementable() {
not this.isSealed() and
not this.getDeclaringType().isSealed() and
(
this.isVirtual() or
this.isOverride() or
this.isAbstract() or
this.getDeclaringType() instanceof Interface
)
}
}
/**
* A parameterizable declaration. Either a callable (`Callable`), a delegate
* type (`DelegateType`), or an indexer (`Indexer`).
*/
class Parameterizable extends Declaration, @parameterizable {
/** Gets raw parameter `i`, including the `this` parameter at index 0. */
Parameter getRawParameter(int i) { params(result, _, _, i, _, this, _) }
/** Gets the `i`th parameter, excluding the `this` parameter. */
Parameter getParameter(int i) { params(result, _, _, i, _, this, _) }
/** Gets the number of parameters of this callable. */
int getNumberOfParameters() { result = count(this.getAParameter()) }
/** Holds if this declaration has no parameters. */
predicate hasNoParameters() { not exists(this.getAParameter()) }
/** Gets a parameter, if any. */
Parameter getAParameter() { result = this.getParameter(_) }
/** Gets a raw parameter (including the qualifier), if any. */
final Parameter getARawParameter() { result = this.getRawParameter(_) }
/**
* Gets the type of the parameter, possibly prefixed
* with `out`, `ref`, or `params`, where appropriate.
*/
private string parameterTypeToString(int i) {
exists(Parameter p, string prefix |
p = this.getParameter(i) and
result = prefix + p.getType().toStringWithTypes()
|
if p.isOut()
then prefix = "out "
else
if p.isRef()
then prefix = "ref "
else
if p.isParams()
then prefix = "params "
else prefix = ""
)
}
/**
* Gets the types of the parameters of this declaration as a
* comma-separated string.
*/
language[monotonicAggregates]
string parameterTypesToString() {
result =
concat(int i | exists(this.getParameter(i)) | this.parameterTypeToString(i), ", " order by i)
}
}