From 02636bcb9971564227fff77fe931b6705254cad1 Mon Sep 17 00:00:00 2001 From: Kushal Pal Date: Sat, 9 Mar 2024 18:30:27 +0530 Subject: [PATCH] borrowck: Implement method translation to BIR. gcc/rust/ChangeLog: * checks/errors/borrowck/rust-bir-builder-expr-stmt.cc (ExprStmtBuilder::visit): Implement function. Signed-off-by: Kushal Pal --- .../borrowck/rust-bir-builder-expr-stmt.cc | 41 ++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/gcc/rust/checks/errors/borrowck/rust-bir-builder-expr-stmt.cc b/gcc/rust/checks/errors/borrowck/rust-bir-builder-expr-stmt.cc index d64641177d0e..bb294d22087b 100644 --- a/gcc/rust/checks/errors/borrowck/rust-bir-builder-expr-stmt.cc +++ b/gcc/rust/checks/errors/borrowck/rust-bir-builder-expr-stmt.cc @@ -281,7 +281,46 @@ ExprStmtBuilder::visit (HIR::CallExpr &expr) void ExprStmtBuilder::visit (HIR::MethodCallExpr &expr) -{} +{ + // TODO : need ExprStmtBuilder::visit_expr (HIR::PathExprSegment) + PlaceId fn = visit_expr (expr.get_method_name ()); + + std::vector arguments = visit_list (expr.get_arguments ()); + + const auto fn_type = ctx.place_db[fn].tyty->as (); + + // compile `self` and handle autoref + PlaceId self = visit_expr (*expr.get_receiver ()); + auto ty = ctx.place_db[self].tyty; + if (ty->get_kind () == TyTy::REF) + { + if (ty->as ()->is_mutable ()) + self + = borrow_place (self, + new TyTy::ReferenceType (ty->get_ref (), + TyTy::TyVar (ty->get_ref ()), + Mutability::Mut)); + else + self + = borrow_place (self, + new TyTy::ReferenceType (ty->get_ref (), + TyTy::TyVar (ty->get_ref ()), + Mutability::Imm)); + } + + arguments.insert (arguments.begin (), self); + + // skip the first parameter i.e `self` + for (size_t i = 1; i < fn_type->get_num_params (); ++i) + { + coercion_site (arguments[i], fn_type->get_param_type_at (i)); + } + + move_all (arguments); + + return_expr (new CallExpr (fn, std::move (arguments)), lookup_type (expr), + true); +} void ExprStmtBuilder::visit (HIR::FieldAccessExpr &expr)