-
Notifications
You must be signed in to change notification settings - Fork 179
Operator overloading #249
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Rustc keeps these intact up to the HIR. After typechecking, while lowering to MIR it generates THIR (typed HIR) on the fly, which does lower it to function calls as necessary I believe. It may also happen during the THIR to MIR lowering. The HIR is always immutable in rustc. |
Generally speaking, the pass could be immutable by just recording the necessary information and delay the actual replacement to the later pass.
I'm afraid the IR recovery back and forth is a bad way to go. We may record the op-func relationship in a table. So we don't have to recover the node. I presume there would be a recursive node replacement, which introduces complexity. |
This is something that will have to be tackled as part of traits in my opinion. |
I think we need to be able to do #442 and #434 with #471 before we can do operator overloading. I suspect there is some GENERIC magic that will help here from C++ or there is an example in libgccjit and operator overloading for primitive types as well. For the first pass this will need to fix #367 as well. Though thinking we also need to fix #241 and I am starting to think we should enforce our HIR to be immutable now since it has been the case so far. Then we can emit THIR so we have a chance during type resolution to fix up type coercions and adjustments properly without resorting to wierd hacks in the GENERIC generation. |
I think THIR will solve a lot of niggly issues for the traits milestone: https://rustc-dev-guide.rust-lang.org/thir.html |
I just made a toy compiler explorer to see how GCC in C++ handles inlining of operator overloading: https://godbolt.org/z/7G4xh5WYE It seems we won't need to mark any operator overloads as inline if optimizations are turned on we get the inlining for free. I have a toy branch working on this, but its only handling custom ADTTypes that implement the add trait, I have some issues with primitive types which also implement the trait at the moment, there might be something to do with lang-items that helps here but I have a lot of buggy code in this branch to deal with first. |
The deref operator overloading is tracked over: #809 |
unary rhs nullptr refactor Fixes #249
Core traits such as the arithmetic operations have generic arguments such as: pub trait Add<Rhs = Self> Addresses #249
Unary operator expressions can be treated as simply having a nullptr rvalue. This patch updates the shared operator overloading code to allow for a nullptr rhs to canonicalize the code path for all operator overloads. Fixes #249
801: operator overloading r=philberty a=philberty This change adds operator overloading by following how the C++ front-end does it. We are relying on GCC to inline the operator overloads which does occur once optimizations are enabled. It also brings back the desurgared compound assignment expression (e2b761b) for lang_items such as add_assign. You can find more information on how the algorithm works in: - c47d5cb - a7fb60b These were refactored in: 0f74fe2 Fixes #249 Co-authored-by: Philip Herron <[email protected]>
Rust supports overloading some operators using traits in
std::ops
orstd::cmp
, as mentioned in the reference.The operators that can be overloaded are:
std::ops::Deref::deref(&x)
orstd::ops::DerefMut::deref_mut(&mut x)
)std::ops::Neg
orstd::ops::Not
)std::ops::
x where x isAdd
,Sub
,Mul
,Div
,Rem
,BitAnd
,BitOr
,BitXor
,Shl
, orShr
)std::cmp::PartialEq::
x where x iseq
,ne
,gt
,lt
,ge
, orle
)std::ops::
xAssign
where x is one of the values listed in ArithmeticOrLogicalExpr)Currently, gccrs does not implement operator overloading and presumably just creates GENERIC/GIMPLE of the operator expression with a type that can't be handled.
One method of implementing operator overloading could be a pass that replaces certain OperatorExprs with function calls. This has some issues:
+=
, for instance, without also overloading operator+
. So the HIR would have to have a separate CompoundAssignmentExpr representation for this reason.Another method of implementing operator overloading could simply be to convert every OperatorExpr to a function call at some point, even ones for primitive types. This would also lead to some issues:
The text was updated successfully, but these errors were encountered: