Skip to content

Commit 147ac37

Browse files
committed
C#: Add downgrade script.
1 parent 29acd69 commit 147ac37

File tree

4 files changed

+3179
-0
lines changed

4 files changed

+3179
-0
lines changed
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
class Expr extends @expr {
2+
string toString() { none() }
3+
}
4+
5+
class Location extends @location {
6+
string toString() { none() }
7+
}
8+
9+
newtype TAddedElement =
10+
TAssignment(Expr e) or
11+
TLhs(Expr e) or
12+
TRhs(Expr e)
13+
14+
module Fresh = QlBuiltins::NewEntity<TAddedElement>;
15+
16+
class TNewExpr = @expr or Fresh::EntityId;
17+
18+
class NewExpr extends TNewExpr {
19+
string toString() { none() }
20+
}
21+
22+
class TNewControlFlowElement = @control_flow_element or Fresh::EntityId;
23+
24+
class NewControlFlowElement extends TNewControlFlowElement {
25+
string toString() { none() }
26+
}
27+
28+
class TypeOrRef extends @type_or_ref {
29+
string toString() { none() }
30+
}
31+
32+
class Callable extends @callable {
33+
string toString() { none() }
34+
}
35+
36+
class Accessible extends @accessible {
37+
string toString() { none() }
38+
}
39+
40+
predicate assignmentKind(int kind) {
41+
// | 63 = @simple_assign_expr
42+
// | 80 = @add_event_expr
43+
// | 81 = @remove_event_expr
44+
// | 83 = @local_var_decl_expr
45+
kind = [63, 80, 81, 83]
46+
}
47+
48+
predicate compoundAssignmentKind(int kind) {
49+
// | 64 = @assign_add_expr
50+
// | 65 = @assign_sub_expr
51+
// | 66 = @assign_mul_expr
52+
// | 67 = @assign_div_expr
53+
// | 68 = @assign_rem_expr
54+
// | 69 = @assign_and_expr
55+
// | 70 = @assign_xor_expr
56+
// | 71 = @assign_or_expr
57+
// | 72 = @assign_lshift_expr
58+
// | 73 = @assign_rshift_expr
59+
// | 119 = @assign_coalesce_expr
60+
// | 134 = @assign_urshift_expr
61+
kind = [64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 119, 134]
62+
}
63+
64+
int getOperatorKindFromAssignmentKind(int kind) {
65+
kind = 64 and result = 44 // @assign_add_expr -> @add_expr
66+
or
67+
kind = 65 and result = 45 // @assign_sub_expr -> @sub_expr
68+
or
69+
kind = 66 and result = 41 // @assign_mul_expr -> @mul_expr
70+
or
71+
kind = 67 and result = 42 // @assign_div_expr -> @div_expr
72+
or
73+
kind = 68 and result = 43 // @assign_rem_expr -> @rem_expr
74+
or
75+
kind = 69 and result = 54 // @assign_and_expr -> @bit_and_expr
76+
or
77+
kind = 70 and result = 55 // @assign_xor_expr -> @bit_xor_expr
78+
or
79+
kind = 71 and result = 56 // @assign_or_expr -> @bit_or_expr
80+
or
81+
kind = 72 and result = 46 // @assign_lshift_expr -> @lshift_expr
82+
or
83+
kind = 73 and result = 47 // @assign_rshift_expr -> @rshift_expr
84+
or
85+
kind = 119 and result = 61 // @assign_coalesce_expr -> @coalesce_expr
86+
or
87+
kind = 134 and result = 133 // @assign_urshift_expr -> @urshift_expr
88+
}
89+
90+
predicate isAssignment(Expr ass) {
91+
exists(int kind | assignmentKind(kind) | expressions(ass, kind, _))
92+
}
93+
94+
class CompoundAssignmentExpr extends Expr {
95+
CompoundAssignmentExpr() {
96+
exists(int kind | compoundAssignmentKind(kind) | expressions(this, kind, _))
97+
}
98+
}
99+
100+
query predicate new_expressions(NewExpr e, int kind, TypeOrRef t) {
101+
expressions(e, kind, t)
102+
or
103+
// Introduce expanded expression nodes.
104+
exists(CompoundAssignmentExpr compound, int kind0, Expr e1, int kind1 |
105+
expressions(compound, kind0, t) and
106+
compoundAssignmentKind(kind0) and
107+
expressions(e1, kind1, _) and
108+
expr_parent(e1, 0, compound)
109+
|
110+
Fresh::map(TAssignment(compound)) = e and kind = 63
111+
or
112+
Fresh::map(TLhs(compound)) = e and kind = kind1
113+
or
114+
Fresh::map(TRhs(compound)) = e and kind = getOperatorKindFromAssignmentKind(kind0)
115+
)
116+
}
117+
118+
query predicate new_expr_parent(NewExpr e, int child, NewControlFlowElement parent) {
119+
if isAssignment(parent)
120+
then
121+
// Swap children for assignments, local variable declarations and add/remove event.
122+
child = 0 and expr_parent(e, 1, parent)
123+
or
124+
child = 1 and expr_parent(e, 0, parent)
125+
else (
126+
exists(CompoundAssignmentExpr compound |
127+
Fresh::map(TAssignment(compound)) = e and child = 2 and parent = compound
128+
or
129+
Fresh::map(TLhs(compound)) = e and child = 1 and parent = Fresh::map(TAssignment(compound))
130+
or
131+
Fresh::map(TRhs(compound)) = e and child = 0 and parent = Fresh::map(TAssignment(compound))
132+
or
133+
expr_parent(e, child, compound) and parent = Fresh::map(TRhs(compound))
134+
)
135+
or
136+
// Copy the expr_parent relation except for compound assignment edges.
137+
expr_parent(e, child, parent) and not parent instanceof CompoundAssignmentExpr
138+
)
139+
}
140+
141+
query predicate new_expr_location(NewExpr e, Location loc) {
142+
expr_location(e, loc)
143+
or
144+
exists(CompoundAssignmentExpr compound |
145+
Fresh::map(TAssignment(compound)) = e and expr_location(compound, loc)
146+
or
147+
Fresh::map(TLhs(compound)) = e and
148+
exists(Expr child | expr_location(child, loc) and expr_parent(child, 0, compound))
149+
or
150+
Fresh::map(TRhs(compound)) = e and expr_location(compound, loc)
151+
)
152+
}
153+
154+
query predicate new_expr_call(NewExpr e, Callable c) {
155+
expr_call(e, c) and not e instanceof CompoundAssignmentExpr
156+
or
157+
exists(CompoundAssignmentExpr compound |
158+
Fresh::map(TRhs(compound)) = e and expr_call(compound, c)
159+
)
160+
}
161+
162+
query predicate new_dynamic_member_name(NewExpr e, string name) {
163+
dynamic_member_name(e, name) and not e instanceof CompoundAssignmentExpr
164+
or
165+
exists(CompoundAssignmentExpr compound |
166+
Fresh::map(TRhs(compound)) = e and dynamic_member_name(compound, name)
167+
)
168+
}
169+
170+
query predicate new_expr_access(NewExpr e, Accessible a) {
171+
expr_access(e, a)
172+
or
173+
exists(CompoundAssignmentExpr compound, Expr access |
174+
expr_parent(access, 0, compound) and
175+
expr_access(access, a) and
176+
Fresh::map(TLhs(compound)) = e
177+
)
178+
}

0 commit comments

Comments
 (0)