]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: refactor resolve_method_address to be more generic
authorPhilip Herron <herron.philip@googlemail.com>
Tue, 23 May 2023 13:35:17 +0000 (14:35 +0100)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 17:37:21 +0000 (18:37 +0100)
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>
gcc/rust/backend/rust-compile-base.cc
gcc/rust/backend/rust-compile-base.h
gcc/rust/backend/rust-compile-expr.cc
gcc/rust/backend/rust-compile-resolve-path.cc

index 6bfc19ee3e35a5454cad0c4a1aeffa32e25bd1a6..f034b8c7b0a40b9869ffd2ffbc1942e306fcb671 100644 (file)
@@ -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);
     }
 
index db47c0f667c59fc1da73e86d6d819f43533d96cf..203705ac730de258157c36ac8a5562b8e5982d66 100644 (file)
@@ -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,
index 78448dd84d08a977f2fc8f9667e42ed247102b15..00b9a07000881a4d1da855443d3a011d9f45750a 100644 (file)
@@ -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;
index 97cfe9c46db18e830836a9297b705d4acea34dff..284f4c6e91e650caef9ec5288d51764d02291008 100644 (file)
@@ -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);
            }
        }