diff options
author | Philip Herron <philip.herron@embecosm.com> | 2021-11-15 18:13:32 +0000 |
---|---|---|
committer | Philip Herron <philip.herron@embecosm.com> | 2021-11-16 15:09:53 +0000 |
commit | 04a49acfa3caa7a85a1a9edd24d69da0ad575ad5 (patch) | |
tree | d6bf4186cb815ff8b0bdf691c9659cf14bca8efd /gcc/rust/backend/rust-compile-expr.cc | |
parent | 0f74fe23c6d602c257ba94b2522bd9d6a594609e (diff) | |
download | gcc-04a49acfa3caa7a85a1a9edd24d69da0ad575ad5.zip gcc-04a49acfa3caa7a85a1a9edd24d69da0ad575ad5.tar.gz gcc-04a49acfa3caa7a85a1a9edd24d69da0ad575ad5.tar.bz2 |
Add negation operator overload support
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
Diffstat (limited to 'gcc/rust/backend/rust-compile-expr.cc')
-rw-r--r-- | gcc/rust/backend/rust-compile-expr.cc | 31 |
1 files changed, 29 insertions, 2 deletions
diff --git a/gcc/rust/backend/rust-compile-expr.cc b/gcc/rust/backend/rust-compile-expr.cc index 8902574..594cfff 100644 --- a/gcc/rust/backend/rust-compile-expr.cc +++ b/gcc/rust/backend/rust-compile-expr.cc @@ -92,6 +92,31 @@ CompileExpr::visit (HIR::CompoundAssignmentExpr &expr) ctx->add_statement (assignment); } +void +CompileExpr::visit (HIR::NegationExpr &expr) +{ + auto op = expr.get_expr_type (); + auto negated_expr = CompileExpr::Compile (expr.get_expr ().get (), ctx); + auto location = expr.get_locus (); + + // this might be an operator overload situation lets check + TyTy::FnType *fntype; + bool is_op_overload = ctx->get_tyctx ()->lookup_operator_overload ( + expr.get_mappings ().get_hirid (), &fntype); + if (is_op_overload) + { + auto lang_item_type + = Analysis::RustLangItem::NegationOperatorToLangItem (op); + translated + = resolve_operator_overload (lang_item_type, expr, negated_expr, + nullptr, expr.get_expr ().get (), nullptr); + return; + } + + translated + = ctx->get_backend ()->negation_expression (op, negated_expr, location); +} + Bexpression * CompileExpr::compile_dyn_dispatch_call (const TyTy::DynamicObjectType *dyn, TyTy::BaseType *receiver, @@ -311,7 +336,8 @@ CompileExpr::resolve_operator_overload ( = static_cast<const TyTy::DynamicObjectType *> (receiver->get_root ()); std::vector<HIR::Expr *> arguments; - arguments.push_back (rhs_expr); + if (rhs_expr != nullptr) // can be null for negation_expr (unary ones) + arguments.push_back (rhs_expr); return compile_dyn_dispatch_call (dyn, receiver, fntype, lhs, arguments, expr.get_locus ()); @@ -356,7 +382,8 @@ CompileExpr::resolve_operator_overload ( std::vector<Bexpression *> args; args.push_back (self); // adjusted self - args.push_back (rhs); + if (rhs != nullptr) // can be null for negation_expr (unary ones) + args.push_back (rhs); auto fncontext = ctx->peek_fn (); return ctx->get_backend ()->call_expression (fncontext.fndecl, fn_expr, args, |