Skip to content

Commit b20ea88

Browse files
committed
Add a utility library for unifying binary ops
BinaryOperations and AssignOperations do not share a common hierarchy. This commit creates a module for unifying the two, and aliases for the common super classes.
1 parent 9bbef6f commit b20ea88

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import cpp
2+
3+
private signature class BinaryOp extends BinaryOperation;
4+
5+
private signature class AssignBinaryOp extends AssignOperation;
6+
7+
/**
8+
* A module for unifying the `BinaryOperation` and `AssignOperation` hierarchies.
9+
*/
10+
private module BinaryOpUnifier<BinaryOp BinaryOpType, AssignBinaryOp AssignBinaryOpType> {
11+
final class FinalExpr = Expr;
12+
13+
class BinaryExpr extends FinalExpr {
14+
BinaryExpr() {
15+
this instanceof BinaryOpType or
16+
this instanceof AssignBinaryOpType
17+
}
18+
19+
Expr getLeftOperand() {
20+
result = this.(BinaryOpType).getLeftOperand()
21+
or
22+
result = this.(AssignBinaryOpType).getLValue()
23+
}
24+
25+
Expr getRightOperand() {
26+
result = this.(BinaryOpType).getRightOperand()
27+
or
28+
result = this.(AssignBinaryOpType).getRValue()
29+
}
30+
31+
string getOperator() {
32+
result = this.(BinaryOpType).getOperator()
33+
or
34+
result = this.(AssignBinaryOpType).getOperator()
35+
}
36+
}
37+
}
38+
39+
/**
40+
* A binary shift operation (`<<` or `>>`).
41+
*/
42+
class BinaryShiftOperation extends BinaryOperation {
43+
BinaryShiftOperation() {
44+
this instanceof LShiftExpr or
45+
this instanceof RShiftExpr
46+
}
47+
}
48+
49+
/**
50+
* A binary shift assignment operation (`<<=` or `>>=`).
51+
*/
52+
class AssignShiftOperation extends AssignOperation {
53+
AssignShiftOperation() {
54+
this instanceof AssignLShiftExpr or
55+
this instanceof AssignRShiftExpr
56+
}
57+
}
58+
59+
/**
60+
* A binary bitwise operation or binary bitwise assignment operation, including shift operations.
61+
*/
62+
class BinaryBitwiseOpOrAssignOp =
63+
BinaryOpUnifier<BinaryBitwiseOperation, AssignBitwiseOperation>::BinaryExpr;
64+
65+
/**
66+
* A binary shift operation (`<<` or `>>`) or shift assignment operation (`<<=` or `>>=`).
67+
*/
68+
class BinaryShiftOpOrAssignOp =
69+
BinaryOpUnifier<BinaryShiftOperation, AssignShiftOperation>::BinaryExpr;
70+
71+
/**
72+
* A binary arithmetic operation or binary arithmetic assignment operation.
73+
*/
74+
class BinaryArithmeticOpOrAssignOp =
75+
BinaryOpUnifier<BinaryArithmeticOperation, AssignArithmeticOperation>::BinaryExpr;

0 commit comments

Comments
 (0)