[gcc r14-7462] gccrs: refactor resolve_method_address to be inside base class

Arthur Cohen cohenarthur@gcc.gnu.org
Tue Jan 16 17:36:49 GMT 2024


https://gcc.gnu.org/g:ae48f884387dee4410643ff04fce1091a699ee13

commit r14-7462-gae48f884387dee4410643ff04fce1091a699ee13
Author: Philip Herron <herron.philip@googlemail.com>
Date:   Tue Apr 4 15:49:20 2023 +0100

    gccrs: refactor resolve_method_address to be inside base class
    
    gcc/rust/ChangeLog:
    
            * backend/rust-compile-base.cc (HIRCompileBase::resolve_method_address): moved here
            * backend/rust-compile-base.h: refactored prototype
            * backend/rust-compile-expr.cc (CompileExpr::resolve_method_address): refactor
            * backend/rust-compile-expr.h: likewise
    
    Signed-off-by: Philip Herron <herron.philip@googlemail.com>

Diff:
---
 gcc/rust/backend/rust-compile-base.cc | 85 +++++++++++++++++++++++++++++++++++
 gcc/rust/backend/rust-compile-base.h  |  6 +++
 gcc/rust/backend/rust-compile-expr.cc | 85 -----------------------------------
 gcc/rust/backend/rust-compile-expr.h  |  6 ---
 4 files changed, 91 insertions(+), 91 deletions(-)

diff --git a/gcc/rust/backend/rust-compile-base.cc b/gcc/rust/backend/rust-compile-base.cc
index 869f05ff28b..23ddc07738c 100644
--- a/gcc/rust/backend/rust-compile-base.cc
+++ b/gcc/rust/backend/rust-compile-base.cc
@@ -27,6 +27,9 @@
 #include "rust-diagnostics.h"
 #include "rust-expr.h"	// for AST::AttrInputLiteral
 #include "rust-macro.h" // for AST::MetaNameValueStr
+#include "rust-hir-path-probe.h"
+#include "rust-type-util.h"
+#include "rust-compile-implitem.h"
 
 #include "fold-const.h"
 #include "stringpool.h"
@@ -739,5 +742,87 @@ HIRCompileBase::named_constant_expression (tree type_tree,
   return decl;
 }
 
+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)
+{
+  // 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);
+  if (resolved_item != nullptr)
+    {
+      if (!fntype->has_subsititions_defined ())
+	return CompileInherentImplItem::Compile (resolved_item, ctx);
+
+      return CompileInherentImplItem::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);
+  HIR::Trait *trait = ctx->get_mappings ()->lookup_trait_item_mapping (
+    trait_item->get_mappings ().get_hirid ());
+
+  Resolver::TraitReference *trait_ref
+    = &Resolver::TraitReference::error_node ();
+  bool ok = ctx->get_tyctx ()->lookup_trait_reference (
+    trait->get_mappings ().get_defid (), &trait_ref);
+  rust_assert (ok);
+
+  // the type resolver can only resolve type bounds to their trait
+  // 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
+
+  auto root = receiver->get_root ();
+  auto candidates
+    = Resolver::PathProbeImplTrait::Probe (root, segment, trait_ref);
+  if (candidates.size () == 0)
+    {
+      // this means we are defaulting back to the trait_item if
+      // possible
+      Resolver::TraitItemReference *trait_item_ref = nullptr;
+      bool ok = trait_ref->lookup_hir_trait_item (*trait_item, &trait_item_ref);
+      rust_assert (ok);				    // found
+      rust_assert (trait_item_ref->is_optional ()); // has definition
+
+      // FIXME Optional means it has a definition and an associated
+      // block which can be a default implementation, if it does not
+      // contain an implementation we should actually return
+      // error_mark_node
+
+      return CompileTraitItem::Compile (trait_item_ref->get_hir_trait_item (),
+					ctx, fntype, true, expr_locus);
+    }
+
+  // FIXME this will be a case to return error_mark_node, there is
+  // an error scenario where a Trait Foo has a method Bar, but this
+  // receiver does not implement this trait or has an incompatible
+  // implementation and we should just return error_mark_node
+
+  rust_assert (candidates.size () == 1);
+  auto &candidate = *candidates.begin ();
+  rust_assert (candidate.is_impl_candidate ());
+  rust_assert (candidate.ty->get_kind () == TyTy::TypeKind::FNDEF);
+  TyTy::FnType *candidate_call = static_cast<TyTy::FnType *> (candidate.ty);
+  HIR::ImplItem *impl_item = candidate.item.impl.impl_item;
+
+  TyTy::BaseType *monomorphized = candidate_call;
+  if (candidate_call->needs_generic_substitutions ())
+    {
+      TyTy::BaseType *infer_impl_call
+	= candidate_call->infer_substitions (expr_locus);
+      monomorphized
+	= Resolver::unify_site (ref, TyTy::TyWithLocation (infer_impl_call),
+				TyTy::TyWithLocation (fntype), expr_locus);
+    }
+
+  return CompileInherentImplItem::Compile (impl_item, ctx, monomorphized);
+}
+
 } // namespace Compile
 } // namespace Rust
diff --git a/gcc/rust/backend/rust-compile-base.h b/gcc/rust/backend/rust-compile-base.h
index 1bf157030be..7a5c9cbb7cc 100644
--- a/gcc/rust/backend/rust-compile-base.h
+++ b/gcc/rust/backend/rust-compile-base.h
@@ -80,6 +80,12 @@ 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,
+			       Location expr_locus);
+
   static void setup_fndecl (tree fndecl, bool is_main_entry_point,
 			    bool is_generic_fn, HIR::Visibility &visibility,
 			    const HIR::FunctionQualifiers &qualifiers,
diff --git a/gcc/rust/backend/rust-compile-expr.cc b/gcc/rust/backend/rust-compile-expr.cc
index 668f704f4b1..e10870463af 100644
--- a/gcc/rust/backend/rust-compile-expr.cc
+++ b/gcc/rust/backend/rust-compile-expr.cc
@@ -18,13 +18,11 @@
 
 #include "rust-compile-expr.h"
 #include "rust-compile-struct-field-expr.h"
-#include "rust-hir-path-probe.h"
 #include "rust-compile-pattern.h"
 #include "rust-compile-resolve-path.h"
 #include "rust-compile-block.h"
 #include "rust-compile-implitem.h"
 #include "rust-constexpr.h"
-#include "rust-type-util.h"
 #include "rust-compile-type.h"
 #include "rust-gcc.h"
 
@@ -1880,89 +1878,6 @@ CompileExpr::get_receiver_from_dyn (const TyTy::DynamicObjectType *dyn,
 						       expr_locus);
 }
 
-tree
-CompileExpr::resolve_method_address (TyTy::FnType *fntype, HirId ref,
-				     TyTy::BaseType *receiver,
-				     HIR::PathIdentSegment &segment,
-				     Analysis::NodeMapping expr_mappings,
-				     Location expr_locus)
-{
-  // 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);
-  if (resolved_item != nullptr)
-    {
-      if (!fntype->has_subsititions_defined ())
-	return CompileInherentImplItem::Compile (resolved_item, ctx);
-
-      return CompileInherentImplItem::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);
-  HIR::Trait *trait = ctx->get_mappings ()->lookup_trait_item_mapping (
-    trait_item->get_mappings ().get_hirid ());
-
-  Resolver::TraitReference *trait_ref
-    = &Resolver::TraitReference::error_node ();
-  bool ok = ctx->get_tyctx ()->lookup_trait_reference (
-    trait->get_mappings ().get_defid (), &trait_ref);
-  rust_assert (ok);
-
-  // the type resolver can only resolve type bounds to their trait
-  // 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
-
-  auto root = receiver->get_root ();
-  auto candidates
-    = Resolver::PathProbeImplTrait::Probe (root, segment, trait_ref);
-  if (candidates.size () == 0)
-    {
-      // this means we are defaulting back to the trait_item if
-      // possible
-      Resolver::TraitItemReference *trait_item_ref = nullptr;
-      bool ok = trait_ref->lookup_hir_trait_item (*trait_item, &trait_item_ref);
-      rust_assert (ok);				    // found
-      rust_assert (trait_item_ref->is_optional ()); // has definition
-
-      // FIXME Optional means it has a definition and an associated
-      // block which can be a default implementation, if it does not
-      // contain an implementation we should actually return
-      // error_mark_node
-
-      return CompileTraitItem::Compile (trait_item_ref->get_hir_trait_item (),
-					ctx, fntype, true, expr_locus);
-    }
-
-  // FIXME this will be a case to return error_mark_node, there is
-  // an error scenario where a Trait Foo has a method Bar, but this
-  // receiver does not implement this trait or has an incompatible
-  // implementation and we should just return error_mark_node
-
-  rust_assert (candidates.size () == 1);
-  auto &candidate = *candidates.begin ();
-  rust_assert (candidate.is_impl_candidate ());
-  rust_assert (candidate.ty->get_kind () == TyTy::TypeKind::FNDEF);
-  TyTy::FnType *candidate_call = static_cast<TyTy::FnType *> (candidate.ty);
-  HIR::ImplItem *impl_item = candidate.item.impl.impl_item;
-
-  TyTy::BaseType *monomorphized = candidate_call;
-  if (candidate_call->needs_generic_substitutions ())
-    {
-      TyTy::BaseType *infer_impl_call
-	= candidate_call->infer_substitions (expr_locus);
-      monomorphized
-	= Resolver::unify_site (ref, TyTy::TyWithLocation (infer_impl_call),
-				TyTy::TyWithLocation (fntype), expr_locus);
-    }
-
-  return CompileInherentImplItem::Compile (impl_item, ctx, monomorphized);
-}
-
 tree
 CompileExpr::resolve_operator_overload (
   Analysis::RustLangItem::ItemType lang_item_type, HIR::OperatorExprMeta expr,
diff --git a/gcc/rust/backend/rust-compile-expr.h b/gcc/rust/backend/rust-compile-expr.h
index fe29c0f690a..52db04c200a 100644
--- a/gcc/rust/backend/rust-compile-expr.h
+++ b/gcc/rust/backend/rust-compile-expr.h
@@ -97,12 +97,6 @@ protected:
 			      TyTy::BaseType *receiver, TyTy::FnType *fntype,
 			      tree receiver_ref, Location expr_locus);
 
-  tree resolve_method_address (TyTy::FnType *fntype, HirId ref,
-			       TyTy::BaseType *receiver,
-			       HIR::PathIdentSegment &segment,
-			       Analysis::NodeMapping expr_mappings,
-			       Location expr_locus);
-
   tree
   resolve_operator_overload (Analysis::RustLangItem::ItemType lang_item_type,
 			     HIR::OperatorExprMeta expr, tree lhs, tree rhs,


More information about the Gcc-cvs mailing list