77private import rust
88private import codeql.rust.elements.internal.ExprImpl:: Impl as ExprImpl
99
10+ /**
11+ * Holds if the given operator is overloaded through the trait `trait` and the
12+ * method name `method`.
13+ */
14+ private predicate isOverloaded ( string op , string path , string method ) {
15+ // Negation
16+ op = "-" and path = "core::ops::arith::Neg" and method = "neg"
17+ or
18+ // Not
19+ op = "!" and path = "core::ops::bit::Not" and method = "not"
20+ or
21+ // Dereference
22+ op = "*" and path = "core::ops::Deref" and method = "deref"
23+ or
24+ // Comparison operators
25+ op = "==" and path = "core::cmp::PartialEq" and method = "eq"
26+ or
27+ op = "!=" and path = "core::cmp::PartialEq" and method = "ne"
28+ or
29+ op = "<" and path = "core::cmp::PartialOrd" and method = "lt"
30+ or
31+ op = "<=" and path = "core::cmp::PartialOrd" and method = "le"
32+ or
33+ op = ">" and path = "core::cmp::PartialOrd" and method = "gt"
34+ or
35+ op = ">=" and path = "core::cmp::PartialOrd" and method = "ge"
36+ or
37+ // Arithmetic operators
38+ op = "+" and path = "core::ops::arith::Add" and method = "add"
39+ or
40+ op = "-" and path = "core::ops::arith::Sub" and method = "sub"
41+ or
42+ op = "*" and path = "core::ops::arith::Mul" and method = "mul"
43+ or
44+ op = "/" and path = "core::ops::arith::Div" and method = "div"
45+ or
46+ op = "%" and path = "core::ops::arith::Rem" and method = "rem"
47+ or
48+ // Arithmetic assignment expressions
49+ op = "+=" and path = "core::ops::arith::AddAssign" and method = "add_assign"
50+ or
51+ op = "-=" and path = "core::ops::arith::SubAssign" and method = "sub_assign"
52+ or
53+ op = "*=" and path = "core::ops::arith::MulAssign" and method = "mul_assign"
54+ or
55+ op = "/=" and path = "core::ops::arith::DivAssign" and method = "div_assign"
56+ or
57+ op = "%=" and path = "core::ops::arith::RemAssign" and method = "rem_assign"
58+ or
59+ // Bitwise operators
60+ op = "&" and path = "core::ops::bit::BitAnd" and method = "bitand"
61+ or
62+ op = "|" and path = "core::ops::bit::BitOr" and method = "bitor"
63+ or
64+ op = "^" and path = "core::ops::bit::BitXor" and method = "bitxor"
65+ or
66+ op = "<<" and path = "core::ops::bit::Shl" and method = "shl"
67+ or
68+ op = ">>" and path = "core::ops::bit::Shr" and method = "shr"
69+ or
70+ // Bitwise assignment operators
71+ op = "&=" and path = "core::ops::bit::BitAndAssign" and method = "bitand_assign"
72+ or
73+ op = "|=" and path = "core::ops::bit::BitOrAssign" and method = "bitor_assign"
74+ or
75+ op = "^=" and path = "core::ops::bit::BitXorAssign" and method = "bitxor_assign"
76+ or
77+ op = "<<=" and path = "core::ops::bit::ShlAssign" and method = "shl_assign"
78+ or
79+ op = ">>=" and path = "core::ops::bit::ShrAssign" and method = "shr_assign"
80+ }
81+
1082/**
1183 * INTERNAL: This module contains the customizable definition of `Operation` and should not
1284 * be referenced directly.
@@ -16,14 +88,28 @@ module Impl {
1688 * An operation, for example `&&`, `+=`, `!` or `*`.
1789 */
1890 abstract class Operation extends ExprImpl:: Expr {
91+ /** Gets the operator name of this operation, if it exists. */
92+ abstract string getOperatorName ( ) ;
93+
94+ /** Gets the `n`th operand of this operation, if any. */
95+ abstract Expr getOperand ( int n ) ;
96+
1997 /**
20- * Gets the operator name of this operation, if it exists.
98+ * Gets the number of operands of this operation.
99+ *
100+ * This is either 1 for prefix operations, or 2 for binary operations.
21101 */
22- abstract string getOperatorName ( ) ;
102+ abstract int getNumberOfOperands ( ) ;
103+
104+ /** Gets an operand of this operation. */
105+ Expr getAnOperand ( ) { result = this .getOperand ( _) }
23106
24107 /**
25- * Gets an operand of this operation.
108+ * Holds if this operation is overloaded to the method `methodName` of the
109+ * trait `trait`.
26110 */
27- abstract Expr getAnOperand ( ) ;
111+ predicate isOverloaded ( Trait trait , string methodName ) {
112+ isOverloaded ( this .getOperatorName ( ) , trait .getCanonicalPath ( ) , methodName )
113+ }
28114 }
29115}
0 commit comments