Skip to content

Commit 8bd12b2

Browse files
committed
C#: Add type(s) for representing global value numbers.
1 parent cc5d565 commit 8bd12b2

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

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

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,66 @@ private GvnKind getKind(ControlFlowElement cfe) {
7070
result = TGvnKindStmt(kind)
7171
)
7272
}
73+
74+
/**
75+
* Type for containing the global value number of a control flow element.
76+
* A global value number, can either be a constant or a list of global value numbers,
77+
* where the list also carries a `kind`, which is used to distinguish between general expressions,
78+
* declarations and statements.
79+
*/
80+
private newtype TGvn =
81+
TConstantGvn(string s) { s = any(Expr e).getValue() } or
82+
TListGvn(GvnList l)
83+
84+
/** The global value number of a control flow element. */
85+
abstract class Gvn extends TGvn {
86+
/** Gets the string representation of this global value number. */
87+
abstract string toString();
88+
}
89+
90+
private class ConstantGvn extends Gvn, TConstantGvn {
91+
override string toString() { this = TConstantGvn(result) }
92+
}
93+
94+
private class ListGvn extends Gvn, TListGvn {
95+
private GvnList l;
96+
97+
ListGvn() { this = TListGvn(l) }
98+
99+
override string toString() { result = "[" + l.toString() + "]" }
100+
}
101+
102+
/**
103+
* Type for containing a list of global value numbers with a kind.
104+
* The empty list carries the kind of the controlflowelement.
105+
*/
106+
private newtype TGvnList =
107+
TGvnNil(GvnKind gkind) or
108+
TGvnCons(Gvn head, GvnList tail) { gvnConstructedCons(_, _, _, head, tail) }
109+
110+
abstract private class GvnList extends TGvnList {
111+
abstract string toString();
112+
}
113+
114+
private class GvnNil extends GvnList, TGvnNil {
115+
private GvnKind kind;
116+
117+
GvnNil() { this = TGvnNil(kind) }
118+
119+
override string toString() { result = "(kind:" + kind + ")" }
120+
}
121+
122+
private class GvnCons extends GvnList, TGvnCons {
123+
private Gvn head;
124+
private GvnList tail;
125+
126+
GvnCons() { this = TGvnCons(head, tail) }
127+
128+
override string toString() { result = head.toString() + " :: " + tail.toString() }
129+
}
130+
131+
private predicate gvnConstructedCons(
132+
ControlFlowElement e, GvnKind kind, int index, Gvn head, GvnList tail
133+
) {
134+
none()
135+
}

0 commit comments

Comments
 (0)