[gcc r14-7616] gccrs: refactor resolve_method_address to be more generic

Arthur Cohen cohenarthur@gcc.gnu.org
Tue Jan 16 17:45:31 GMT 2024


https://gcc.gnu.org/g:636e9b41035b55a138065398490ad9007b10fe06

commit r14-7616-g636e9b41035b55a138065398490ad9007b10fe06
Author: Philip Herron <herron.philip@googlemail.com>
Date:   Tue May 23 14:35:17 2023 +0100

    gccrs: refactor resolve_method_address to be more generic
    
    We can reuse the DefId to lookup the mappings to Items or Trait Items
    instead of the HirId this is more generic and can then be reused for the
    deref operator overload during autoderef cycles.
    
    Addresses #2190
    
    gcc/rust/ChangeLog:
    
            * backend/rust-compile-base.cc (HIRCompileBase::resolve_method_address): refactor
            * backend/rust-compile-base.h: likewise
            * backend/rust-compile-expr.cc (CompileExpr::visit): likewise
            * backend/rust-compile-resolve-path.cc (HIRCompileBase::query_compile): likewise
    
    Signed-off-by: Philip Herron <herron.philip@googlemail.com>

Diff:
---
 gcc/rust/backend/rust-compile-base.cc         | 24 +++++++++++++-----------
 gcc/rust/backend/rust-compile-base.h          |  5 +----
 gcc/rust/backend/rust-compile-expr.cc         |  8 ++------
 gcc/rust/backend/rust-compile-resolve-path.cc |  3 +--
 4 files changed, 17 insertions(+), 23 deletions(-)

diff --git a/gcc/rust/backend/rust-compile-base.cc b/gcc/rust/backend/rust-compile-base.cc
index 6bfc19ee3e3..f034b8c7b0a 100644
--- a/gcc/rust/backend/rust-compile-base.cc
+++ b/gcc/rust/backend/rust-compile-base.cc
@@ -766,27 +766,28 @@ HIRCompileBase::named_constant_expression (tree type_tree,
 }
 
 tree
-HIRCompileBase::resolve_method_address (
-  TyTy::FnType *fntype, HirId ref, TyTy::BaseType *receiver,
-  const HIR::PathIdentSegment &segment,
-  const Analysis::NodeMapping &expr_mappings, Location expr_locus)
+HIRCompileBase::resolve_method_address (TyTy::FnType *fntype,
+					TyTy::BaseType *receiver,
+					Location expr_locus)
 {
+  DefId id = fntype->get_id ();
+  rust_assert (id != UNKNOWN_DEFID);
+
   // Now we can try and resolve the address since this might be a forward
   // declared function, generic function which has not be compiled yet or
   // its an not yet trait bound function
-  HIR::ImplItem *resolved_item
-    = ctx->get_mappings ()->lookup_hir_implitem (ref, nullptr);
+  HIR::Item *resolved_item = ctx->get_mappings ()->lookup_defid (id);
   if (resolved_item != nullptr)
     {
       if (!fntype->has_subsititions_defined ())
-	return CompileInherentImplItem::Compile (resolved_item, ctx);
+	return CompileItem::compile (resolved_item, ctx);
 
-      return CompileInherentImplItem::Compile (resolved_item, ctx, fntype);
+      return CompileItem::compile (resolved_item, ctx, fntype);
     }
 
   // it might be resolved to a trait item
   HIR::TraitItem *trait_item
-    = ctx->get_mappings ()->lookup_hir_trait_item (ref);
+    = ctx->get_mappings ()->lookup_trait_item_defid (id);
   HIR::Trait *trait = ctx->get_mappings ()->lookup_trait_item_mapping (
     trait_item->get_mappings ().get_hirid ());
 
@@ -800,7 +801,7 @@ HIRCompileBase::resolve_method_address (
   // item so its up to us to figure out if this path should resolve
   // to an trait-impl-block-item or if it can be defaulted to the
   // trait-impl-item's definition
-
+  const HIR::PathIdentSegment segment (trait_item->trait_identifier ());
   auto root = receiver->get_root ();
   auto candidates
     = Resolver::PathProbeImplTrait::Probe (root, segment, trait_ref);
@@ -840,7 +841,8 @@ HIRCompileBase::resolve_method_address (
       TyTy::BaseType *infer_impl_call
 	= candidate_call->infer_substitions (expr_locus);
       monomorphized
-	= Resolver::unify_site (ref, TyTy::TyWithLocation (infer_impl_call),
+	= Resolver::unify_site (fntype->get_ref (),
+				TyTy::TyWithLocation (infer_impl_call),
 				TyTy::TyWithLocation (fntype), expr_locus);
     }
 
diff --git a/gcc/rust/backend/rust-compile-base.h b/gcc/rust/backend/rust-compile-base.h
index db47c0f667c..203705ac730 100644
--- a/gcc/rust/backend/rust-compile-base.h
+++ b/gcc/rust/backend/rust-compile-base.h
@@ -80,10 +80,7 @@ protected:
   tree resolve_unsized_dyn_adjustment (Resolver::Adjustment &adjustment,
 				       tree expression, Location locus);
 
-  tree resolve_method_address (TyTy::FnType *fntype, HirId ref,
-			       TyTy::BaseType *receiver,
-			       const HIR::PathIdentSegment &segment,
-			       const Analysis::NodeMapping &expr_mappings,
+  tree resolve_method_address (TyTy::FnType *fntype, TyTy::BaseType *receiver,
 			       Location expr_locus);
 
   void compile_function_body (tree fndecl, HIR::BlockExpr &function_body,
diff --git a/gcc/rust/backend/rust-compile-expr.cc b/gcc/rust/backend/rust-compile-expr.cc
index 78448dd84d0..00b9a070008 100644
--- a/gcc/rust/backend/rust-compile-expr.cc
+++ b/gcc/rust/backend/rust-compile-expr.cc
@@ -1757,9 +1757,7 @@ CompileExpr::visit (HIR::MethodCallExpr &expr)
       // lookup compiled functions since it may have already been compiled
       HIR::PathExprSegment method_name = expr.get_method_name ();
       HIR::PathIdentSegment segment_name = method_name.get_segment ();
-      fn_expr
-	= resolve_method_address (fntype, ref, receiver, segment_name,
-				  expr.get_mappings (), expr.get_locus ());
+      fn_expr = resolve_method_address (fntype, receiver, expr.get_locus ());
     }
 
   // lookup the autoderef mappings
@@ -1899,9 +1897,7 @@ CompileExpr::resolve_operator_overload (
   // lookup compiled functions since it may have already been compiled
   HIR::PathIdentSegment segment_name (
     Analysis::RustLangItem::ToString (lang_item_type));
-  tree fn_expr
-    = resolve_method_address (fntype, ref, receiver, segment_name,
-			      expr.get_mappings (), expr.get_locus ());
+  tree fn_expr = resolve_method_address (fntype, receiver, expr.get_locus ());
 
   // lookup the autoderef mappings
   std::vector<Resolver::Adjustment> *adjustments = nullptr;
diff --git a/gcc/rust/backend/rust-compile-resolve-path.cc b/gcc/rust/backend/rust-compile-resolve-path.cc
index 97cfe9c46db..284f4c6e91e 100644
--- a/gcc/rust/backend/rust-compile-resolve-path.cc
+++ b/gcc/rust/backend/rust-compile-resolve-path.cc
@@ -213,8 +213,7 @@ HIRCompileBase::query_compile (HirId ref, TyTy::BaseType *lookup,
 	      receiver = fn->get_self_type ();
 	      receiver = receiver->destructure ();
 
-	      return resolve_method_address (fn, ref, receiver, final_segment,
-					     mappings, expr_locus);
+	      return resolve_method_address (fn, receiver, expr_locus);
 	    }
 	}


More information about the Gcc-cvs mailing list