C#: Teach Implements.qll about nested types

This commit is contained in:
Tom Hvitved
2020-04-24 15:10:27 +02:00
parent f9ece0aefb
commit 0466e36985
4 changed files with 54 additions and 14 deletions

View File

@@ -250,7 +250,7 @@ private module Gvn {
private class LeafType extends Type {
LeafType() {
not exists(this.getAChild()) and
not this instanceof Unification::GenericType and
not this instanceof MethodTypeParameter and
not this instanceof DynamicType
}
@@ -267,7 +267,9 @@ private module Gvn {
gvnConstructedCons(_, _, _, head, tail)
}
private ConstructedGvnTypeList gvnConstructed(Type t, Unification::CompoundTypeKind k, int i) {
private ConstructedGvnTypeList gvnConstructed(
Unification::GenericType t, Unification::CompoundTypeKind k, int i
) {
result = TConstructedGvnTypeNil(k) and
i = -1 and
k = Unification::getTypeKind(t)
@@ -278,14 +280,17 @@ private module Gvn {
}
pragma[noinline]
private GvnType gvnTypeChild(Type t, int i) { result = getGlobalValueNumber(t.getChild(i)) }
private GvnType gvnTypeChildExt(Unification::GenericType t, int i) {
result = getGlobalValueNumber(t.getChildExt(i))
}
pragma[noinline]
private predicate gvnConstructedCons(
Type t, Unification::CompoundTypeKind k, int i, GvnType head, ConstructedGvnTypeList tail
Unification::GenericType t, Unification::CompoundTypeKind k, int i, GvnType head,
ConstructedGvnTypeList tail
) {
tail = gvnConstructed(t, k, i - 1) and
head = gvnTypeChild(t, i)
head = gvnTypeChildExt(t, i)
}
/** Gets the global value number for a given type. */
@@ -319,6 +324,8 @@ private module Gvn {
}
private class ConstructedGvnTypeList extends TConstructedGvnTypeList {
Unification::CompoundTypeKind getKind() { this = gvnConstructed(_, result, _) }
private int length() {
this = TConstructedGvnTypeNil(_) and result = -1
or
@@ -338,17 +345,47 @@ private module Gvn {
)
}
/**
* Gets a textual representation of this constructed type, restricted
* to the prefix `t` of the underlying source declaration type.
*
* The `toString()` calculation needs to be split up into prefixes, in
* order to apply the type arguments correctly. For example, a source
* declaration type `A<>.B.C<,>` applied to types `int, string, bool`
* needs to be printed as `A<int>.B.C<string,bool>`.
*/
language[monotonicAggregates]
private string toStringConstructed(Unification::GenericType t) {
t = this.getKind().getConstructedSourceDeclaration().getQualifier*() and
exists(int offset, int children, string name, string nameArgs |
offset = t.getNumberOfQualifierChildrenExt() and
children = t.getNumberOfChildrenSelf() and
name = Unification::getQualifiedName(t) and
if children = 0
then nameArgs = name
else
exists(string offsetArgs |
offsetArgs =
concat(int i |
i in [offset .. offset + children - 1]
|
this.getArg(i).toString(), "," order by i
) and
nameArgs = name.prefix(name.length() - children - 1) + "<" + offsetArgs + ">"
)
|
offset = 0 and result = nameArgs
or
result = this.toStringConstructed(t.getQualifier()) + "." + nameArgs
)
}
language[monotonicAggregates]
string toString() {
exists(Unification::CompoundTypeKind k, string args |
this = gvnConstructed(_, k, _) and
args =
concat(int i |
i in [0 .. k.getNumberOfTypeParameters() - 1]
|
this.getArg(i).toString(), "," order by i
) and
result = k.toString(args)
exists(Unification::CompoundTypeKind k | k = this.getKind() |
result = k.toStringBuiltin(this.getArg(0).toString())
or
result = this.toStringConstructed(k.getConstructedSourceDeclaration())
)
}

View File

@@ -27,3 +27,4 @@
| overrides.cs:268:29:268:36 | Property | overrides.cs:216:13:216:20 | Property |
| overrides.cs:269:29:269:32 | Item | overrides.cs:217:13:217:16 | Item |
| overrides.cs:270:44:270:48 | Event | overrides.cs:218:28:218:32 | Event |
| overrides.cs:284:25:284:28 | M | overrides.cs:279:18:279:21 | M |

View File

@@ -9,3 +9,4 @@
| overrides.cs:249:22:249:25 | M | overrides.cs:247:11:247:12 | A6 | overrides.cs:162:11:162:14 | M | overrides.cs:160:22:160:26 | I2<Object[]> |
| overrides.cs:259:27:259:30 | M | overrides.cs:257:11:257:12 | A8 | overrides.cs:223:26:223:29 | M | overrides.cs:221:11:221:12 | A1 |
| overrides.cs:267:27:267:30 | M | overrides.cs:265:11:265:12 | A9 | overrides.cs:223:26:223:29 | M | overrides.cs:221:11:221:12 | A1 |
| overrides.cs:284:25:284:28 | M | overrides.cs:282:15:282:17 | A10 | overrides.cs:279:18:279:21 | M | overrides.cs:277:19:277:20 | I6 |

View File

@@ -47,3 +47,4 @@
| overrides.G2.M<S>(string, S) | overrides.G.M<S>(string, S) | overrides |
| overrides.G.M<S>(string, S) | overrides.I2<String>.M<S>(string, S) | implements |
| overrides.H<>.M<S>(TA, S) | overrides.I2<TA>.M<S>(TA, S) | implements |
| overrides.Outer<>.A10.M<T>(Inner) | overrides.Outer<>.I6.M<T>(Inner) | implements |