Skip to content

Commit 38f04e5

Browse files
committed
C#: Flatten the the Gvn type.
1 parent 6b4dea7 commit 38f04e5

File tree

2 files changed

+144
-166
lines changed

2 files changed

+144
-166
lines changed

csharp/ql/lib/semmle/code/csharp/commons/StructuralComparison.qll

Lines changed: 21 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ private boolean isTargetThis(Expr e) {
5151
result = false and not e.(MemberAccess).targetIsThisInstance()
5252
}
5353

54-
/** Gets the AST node kind of element `cfe` wrapped in the GvnKind type. */
54+
/** Gets the AST node kind of element `cfe` wrapped in the `GvnKind` type. */
5555
private GvnKind getKind(ControlFlowElement cfe) {
5656
exists(int kind |
5757
expressions(cfe, kind, _) and
@@ -72,33 +72,21 @@ private class ConstantGvn extends Gvn, TConstantGvn {
7272
override string toString() { this = TConstantGvn(result) }
7373
}
7474

75-
private class ListGvn extends Gvn, TListGvn {
76-
private GvnList l;
77-
78-
ListGvn() { this = TListGvn(l) }
79-
80-
override string toString() { result = "[" + l.toString() + "]" }
81-
}
82-
83-
abstract private class GvnList extends TGvnList {
84-
abstract string toString();
85-
}
86-
87-
private class GvnNil extends GvnList, TGvnNil {
75+
private class GvnBase extends Gvn, TGvnBase {
8876
private GvnKind kind;
8977

90-
GvnNil() { this = TGvnNil(kind) }
78+
GvnBase() { this = TGvnBase(kind) }
9179

9280
override string toString() { result = "(kind:" + kind + ")" }
9381
}
9482

95-
private class GvnCons extends GvnList, TGvnCons {
83+
private class GvnStruct extends Gvn, TGvnStruct {
9684
private Gvn head;
97-
private GvnList tail;
85+
private Gvn tail;
9886

99-
GvnCons() { this = TGvnCons(head, tail) }
87+
GvnStruct() { this = TGvnStruct(head, tail) }
10088

101-
override string toString() { result = head.toString() + " :: " + tail.toString() }
89+
override string toString() { result = "(" + head.toString() + " :: " + tail.toString() + ")" }
10290
}
10391

10492
/**
@@ -118,14 +106,14 @@ private GvnKind getGvnKind(ControlFlowElement cfe) {
118106
)
119107
}
120108

121-
private GvnList gvnConstructed(ControlFlowElement cfe, GvnKind kind, int index) {
109+
private Gvn gvnConstructed(ControlFlowElement cfe, GvnKind kind, int index) {
122110
kind = getGvnKind(cfe) and
123-
result = TGvnNil(kind) and
111+
result = TGvnBase(kind) and
124112
index = -1
125113
or
126-
exists(Gvn head, GvnList tail |
127-
gvnConstructedCons(cfe, kind, index, head, tail) and
128-
result = TGvnCons(head, tail)
114+
exists(Gvn head, Gvn tail |
115+
gvnConstructedStruct(cfe, kind, index, head, tail) and
116+
result = TGvnStruct(head, tail)
129117
)
130118
}
131119

@@ -155,8 +143,8 @@ private Gvn gvnChild(ControlFlowElement cfe, int index) {
155143
}
156144

157145
pragma[noinline]
158-
private predicate gvnConstructedCons(
159-
ControlFlowElement cfe, GvnKind kind, int index, Gvn head, GvnList tail
146+
private predicate gvnConstructedStruct(
147+
ControlFlowElement cfe, GvnKind kind, int index, Gvn head, Gvn tail
160148
) {
161149
tail = gvnConstructed(cfe, kind, index - 1) and
162150
head = gvnChild(cfe, index)
@@ -176,23 +164,14 @@ private module Cached {
176164

177165
/**
178166
* Type for containing the global value number of a control flow element.
179-
* A global value number, can either be a constant or a list of global value numbers,
180-
* where the list also carries a `kind`, which is used to distinguish between general expressions,
181-
* declarations and statements.
167+
* A global value number, can either be a constant, a kind or a structure containing multiple global value numbers.
168+
* The construction of the type produces a list like structure.
182169
*/
183170
cached
184171
newtype TGvn =
185172
TConstantGvn(string s) { s = any(Expr e).getValue() } or
186-
TListGvn(GvnList l)
187-
188-
/**
189-
* Type for containing a list of global value numbers with a kind.
190-
* The empty list carries the kind of the controlflowelement.
191-
*/
192-
cached
193-
newtype TGvnList =
194-
TGvnNil(GvnKind gkind) or
195-
TGvnCons(Gvn head, GvnList tail) { gvnConstructedCons(_, _, _, head, tail) }
173+
TGvnBase(GvnKind gkind) or
174+
TGvnStruct(Gvn head, Gvn tail) { gvnConstructedStruct(_, _, _, head, tail) }
196175
}
197176

198177
private import Cached
@@ -203,10 +182,9 @@ Gvn toGvn(ControlFlowElement cfe) {
203182
result = TConstantGvn(cfe.(Expr).getValue())
204183
or
205184
not exists(cfe.(Expr).getValue()) and
206-
exists(GvnList l, GvnKind kind, int index |
207-
l = gvnConstructed(cfe, kind, index - 1) and
208-
index = getNumberOfActualChildren(cfe) and
209-
result = TListGvn(l)
185+
exists(GvnKind kind, int index |
186+
result = gvnConstructed(cfe, kind, index - 1) and
187+
index = getNumberOfActualChildren(cfe)
210188
)
211189
}
212190

0 commit comments

Comments
 (0)