]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: refactor resolve_method_address to be inside base class
authorPhilip Herron <herron.philip@googlemail.com>
Tue, 4 Apr 2023 14:49:20 +0000 (15:49 +0100)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 17:28:45 +0000 (18:28 +0100)
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>
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-expr.h

index 869f05ff28b7f61a42c3c93817261ee35c801ce5..23ddc07738c010b3a5cba901e8e1e46ff4d2bfc3 100644 (file)
@@ -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
index 1bf157030be3557650eed23338460fac4ffad339..7a5c9cbb7cc74698857fdad773a750268a3e8cfa 100644 (file)
@@ -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,
index 668f704f4b1328d37001a3676dc6c439202bf98c..e10870463afe6d47676c9d3e6dabc343d9b10063 100644 (file)
 
 #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,
index fe29c0f690a1af97c032f70d32a16cf605893bef..52db04c200a590901315e9357701fa6f70e8fb60 100644 (file)
@@ -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,