From: Lishin <[email protected]>

Refactor helper code into separate rust-compile-drop files.
This keeps the Drop-related logic in one place.

gcc/rust/ChangeLog:

        * Make-lang.in: Add rust-compile-drop.o.
        * backend/rust-compile-block.cc (compile_drop_call): Move to 
CompileDrop.
        (CompileBlock::visit): Use CompileDrop to emit scope drop calls.
        * backend/rust-compile-context.h (struct DropCandidate): Move to 
rust-compile-drop.h.
        * backend/rust-compile-pattern.cc (type_has_drop_impl): Move to 
CompileDrop.
        (CompilePatternLet::visit): Use CompileDrop to check Drop impls.
        * backend/rust-compile-drop.cc: New file.
        * backend/rust-compile-drop.h: New file.

Signed-off-by: Lishin <[email protected]>
---
This change was merged into the gccrs repository and is posted here for
upstream visibility and potential drive-by review, as requested by GCC
release managers.
Each commit email contains a link to its details on github from where you can
find the Pull-Request and associated discussions.


Commit on github: 
https://github.com/Rust-GCC/gccrs/commit/4e3750bd6e6a67c33f1c0d1c3e992c77ebd9dd2b

The commit has NOT been mentioned in any issue.

The commit has been mentioned in the following pull-request(s):
 - https://github.com/Rust-GCC/gccrs/pull/4564

 gcc/rust/Make-lang.in                    |  1 +
 gcc/rust/backend/rust-compile-block.cc   | 62 +--------------
 gcc/rust/backend/rust-compile-context.h  |  9 +--
 gcc/rust/backend/rust-compile-drop.cc    | 97 ++++++++++++++++++++++++
 gcc/rust/backend/rust-compile-drop.h     | 42 ++++++++++
 gcc/rust/backend/rust-compile-pattern.cc | 35 ++-------
 6 files changed, 152 insertions(+), 94 deletions(-)
 create mode 100644 gcc/rust/backend/rust-compile-drop.cc
 create mode 100644 gcc/rust/backend/rust-compile-drop.h

diff --git a/gcc/rust/Make-lang.in b/gcc/rust/Make-lang.in
index b4a042bda..5a7da1cfe 100644
--- a/gcc/rust/Make-lang.in
+++ b/gcc/rust/Make-lang.in
@@ -225,6 +225,7 @@ GRS_OBJS = \
     rust/rust-compile-struct-field-expr.o \
     rust/rust-constexpr.o \
     rust/rust-compile-base.o \
+    rust/rust-compile-drop.o \
     rust/rust-tree.o \
     rust/rust-compile-context.o \
     rust/rust-export-metadata.o \
diff --git a/gcc/rust/backend/rust-compile-block.cc 
b/gcc/rust/backend/rust-compile-block.cc
index 21ee37c48..245c16c52 100644
--- a/gcc/rust/backend/rust-compile-block.cc
+++ b/gcc/rust/backend/rust-compile-block.cc
@@ -17,53 +17,14 @@
 // <http://www.gnu.org/licenses/>.
 
 #include "rust-compile-block.h"
-#include "rust-compile-stmt.h"
+#include "rust-compile-drop.h"
 #include "rust-compile-expr.h"
-#include "rust-compile-implitem.h"
+#include "rust-compile-stmt.h"
 #include "rust-hir-expr.h"
-#include "rust-hir-path-probe.h"
-#include "rust-lang-item.h"
 
 namespace Rust {
 namespace Compile {
 
-static tree
-compile_drop_call (Context *ctx, Bvariable *var, TyTy::BaseType *ty,
-                  location_t locus)
-{
-  auto drop_lang = ctx->get_mappings ().lookup_lang_item 
(LangItem::Kind::DROP);
-  if (!drop_lang.has_value ())
-    return NULL_TREE;
-
-  Resolver::TraitReference *drop_ref = nullptr;
-  bool ok
-    = ctx->get_tyctx ()->lookup_trait_reference (drop_lang.value (), 
&drop_ref);
-  if (!ok)
-    return NULL_TREE;
-
-  HIR::PathIdentSegment segment ("drop");
-  auto candidates
-    = Resolver::PathProbeImplTrait::Probe (ty->get_root (), segment, drop_ref);
-
-  for (auto &candidate : candidates)
-    {
-      if (!candidate.is_impl_candidate ()
-         || candidate.ty->get_kind () != TyTy::TypeKind::FNDEF)
-       continue;
-
-      auto *fn_type = static_cast<TyTy::FnType *> (candidate.ty);
-      tree fn_addr
-       = CompileInherentImplItem::Compile (candidate.item.impl.impl_item, ctx,
-                                           fn_type, locus);
-
-      tree var_expr = Backend::var_expression (var, locus);
-      tree var_addr = HIRCompileBase::address_expression (var_expr, locus);
-
-      return Backend::call_expression (fn_addr, {var_addr}, nullptr, locus);
-    }
-  return NULL_TREE;
-}
-
 CompileBlock::CompileBlock (Context *ctx, Bvariable *result)
   : HIRCompileBase (ctx), translated (nullptr), result (result)
 {}
@@ -124,24 +85,7 @@ CompileBlock::visit (HIR::BlockExpr &expr)
                                         expr.get_locus ());
       ctx->add_statement (assignment);
     }
-
-  auto &drop_candidates = ctx->peek_block_drop_candidates ();
-
-  for (auto it = drop_candidates.rbegin (); it != drop_candidates.rend (); 
++it)
-    {
-      TyTy::BaseType *ty = nullptr;
-      Bvariable *var = nullptr;
-
-      bool ok = ctx->get_tyctx ()->lookup_type (it->hirid, &ty);
-      rust_assert (ok);
-
-      ok = ctx->lookup_var_decl (it->hirid, &var);
-      rust_assert (ok);
-
-      tree drop_call = compile_drop_call (ctx, var, ty, it->locus);
-      if (drop_call != NULL_TREE)
-       ctx->add_statement (convert_to_void (drop_call, ICV_STATEMENT));
-    }
+  CompileDrop::emit_current_scope_drop_calls (ctx);
 
   ctx->pop_block ();
   translated = new_block;
diff --git a/gcc/rust/backend/rust-compile-context.h 
b/gcc/rust/backend/rust-compile-context.h
index 4f541d1b3..b50326a3a 100644
--- a/gcc/rust/backend/rust-compile-context.h
+++ b/gcc/rust/backend/rust-compile-context.h
@@ -20,6 +20,7 @@
 #define RUST_COMPILE_CONTEXT
 
 #include "rust-system.h"
+#include "rust-compile-drop.h"
 #include "rust-hir-map.h"
 #include "rust-name-resolver.h"
 #include "rust-hir-type-check.h"
@@ -50,12 +51,6 @@ struct CustomDeriveInfo
   std::vector<std::string> attributes;
 };
 
-struct DropCandidate
-{
-  HirId hirid;
-  location_t locus;
-};
-
 class Context
 {
 public:
@@ -150,7 +145,7 @@ public:
   void note_simple_drop_candidate (HirId hirid, location_t locus)
   {
     rust_assert (!block_drop_candidates.empty ());
-    block_drop_candidates.back ().push_back ({hirid, locus});
+    block_drop_candidates.back ().emplace_back (hirid, locus);
   }
 
   void insert_var_decl (HirId id, ::Bvariable *decl)
diff --git a/gcc/rust/backend/rust-compile-drop.cc 
b/gcc/rust/backend/rust-compile-drop.cc
new file mode 100644
index 000000000..6d24c277c
--- /dev/null
+++ b/gcc/rust/backend/rust-compile-drop.cc
@@ -0,0 +1,97 @@
+#include "rust-compile-drop.h"
+#include "rust-compile-base.h"
+#include "rust-compile-context.h"
+#include "rust-compile-implitem.h"
+#include "rust-hir-path-probe.h"
+#include "rust-hir-trait-reference.h"
+#include "rust-hir-type-bounds.h"
+#include "rust-lang-item.h"
+#include "rust-tyty.h"
+
+namespace Rust {
+namespace Compile {
+
+bool
+CompileDrop::type_has_drop_impl (Context *ctx, TyTy::BaseType *ty)
+{
+  auto drop_lang_item
+    = ctx->get_mappings ().lookup_lang_item (LangItem::Kind::DROP);
+
+  if (!drop_lang_item.has_value ())
+    return false;
+
+  DefId drop_id = drop_lang_item.value ();
+
+  auto candidates = Resolver::TypeBoundsProbe::Probe (ty);
+  for (auto &candidate : candidates)
+    {
+      Resolver::TraitReference *trait_ref = candidate.first;
+      if (trait_ref != nullptr && trait_ref->get_defid () == drop_id)
+       return true;
+    }
+
+  return false;
+}
+
+// Find the Drop trait, look for the drop method, and build the function call.
+tree
+CompileDrop::compile_drop_call (Context *ctx, Bvariable *var,
+                               TyTy::BaseType *ty, location_t locus)
+{
+  auto drop_lang = ctx->get_mappings ().lookup_lang_item 
(LangItem::Kind::DROP);
+  if (!drop_lang.has_value ())
+    return NULL_TREE;
+
+  Resolver::TraitReference *drop_ref = nullptr;
+  bool ok
+    = ctx->get_tyctx ()->lookup_trait_reference (drop_lang.value (), 
&drop_ref);
+  if (!ok)
+    return NULL_TREE;
+
+  HIR::PathIdentSegment segment ("drop");
+  auto candidates
+    = Resolver::PathProbeImplTrait::Probe (ty->get_root (), segment, drop_ref);
+
+  for (auto &candidate : candidates)
+    {
+      if (!candidate.is_impl_candidate ()
+         || candidate.ty->get_kind () != TyTy::TypeKind::FNDEF)
+       continue;
+
+      auto *fn_type = static_cast<TyTy::FnType *> (candidate.ty);
+      tree fn_addr
+       = CompileInherentImplItem::Compile (candidate.item.impl.impl_item, ctx,
+                                           fn_type, locus);
+
+      tree var_expr = Backend::var_expression (var, locus);
+      tree var_addr = HIRCompileBase::address_expression (var_expr, locus);
+
+      return Backend::call_expression (fn_addr, {var_addr}, nullptr, locus);
+    }
+  return NULL_TREE;
+}
+
+void
+CompileDrop::emit_current_scope_drop_calls (Context *ctx)
+{
+  auto &drop_candidates = ctx->peek_block_drop_candidates ();
+
+  for (auto it = drop_candidates.rbegin (); it != drop_candidates.rend (); 
++it)
+    {
+      TyTy::BaseType *ty = nullptr;
+      Bvariable *var = nullptr;
+
+      bool ok = ctx->get_tyctx ()->lookup_type (it->hirid, &ty);
+      rust_assert (ok);
+
+      ok = ctx->lookup_var_decl (it->hirid, &var);
+      rust_assert (ok);
+
+      tree drop_call = CompileDrop::compile_drop_call (ctx, var, ty, 
it->locus);
+      if (drop_call != NULL_TREE)
+       ctx->add_statement (convert_to_void (drop_call, ICV_STATEMENT));
+    }
+}
+
+} // namespace Compile
+} // namespace Rust
diff --git a/gcc/rust/backend/rust-compile-drop.h 
b/gcc/rust/backend/rust-compile-drop.h
new file mode 100644
index 000000000..e16baab7e
--- /dev/null
+++ b/gcc/rust/backend/rust-compile-drop.h
@@ -0,0 +1,42 @@
+#ifndef RUST_COMPILE_DROP
+#define RUST_COMPILE_DROP
+
+#include "rust-system.h"
+#include "rust-hir-map.h"
+
+class Bvariable;
+
+namespace Rust {
+
+namespace TyTy {
+class BaseType;
+}
+
+namespace Compile {
+
+class Context;
+
+struct DropCandidate
+{
+  DropCandidate (HirId hirid, location_t locus) : hirid (hirid), locus (locus)
+  {}
+
+  HirId hirid;
+  location_t locus;
+};
+
+class CompileDrop
+{
+public:
+  static bool type_has_drop_impl (Context *ctx, TyTy::BaseType *ty);
+
+  static tree compile_drop_call (Context *ctx, Bvariable *var,
+                                TyTy::BaseType *ty, location_t locus);
+
+  static void emit_current_scope_drop_calls (Context *ctx);
+};
+
+} // namespace Compile
+} // namespace Rust
+
+#endif // RUST_COMPILE_DROP
\ No newline at end of file
diff --git a/gcc/rust/backend/rust-compile-pattern.cc 
b/gcc/rust/backend/rust-compile-pattern.cc
index 021e438b6..2179916d5 100644
--- a/gcc/rust/backend/rust-compile-pattern.cc
+++ b/gcc/rust/backend/rust-compile-pattern.cc
@@ -17,46 +17,25 @@
 // <http://www.gnu.org/licenses/>.
 
 #include "rust-compile-pattern.h"
+#include "print-tree.h"
+#include "rust-compile-drop.h"
 #include "rust-compile-expr.h"
 #include "rust-compile-resolve-path.h"
-#include "rust-constexpr.h"
 #include "rust-compile-type.h"
-#include "print-tree.h"
+#include "rust-constexpr.h"
 #include "rust-diagnostics.h"
 #include "rust-hir-pattern-abstract.h"
 #include "rust-hir-pattern.h"
-#include "rust-system.h"
-#include "rust-tyty.h"
-#include "rust-hir-type-bounds.h"
 #include "rust-hir-trait-reference.h"
+#include "rust-hir-type-bounds.h"
 #include "rust-lang-item.h"
+#include "rust-system.h"
+#include "rust-tyty.h"
 #include "tree.h"
 
 namespace Rust {
 namespace Compile {
 
-static bool
-type_has_drop_impl (Context *ctx, TyTy::BaseType *ty)
-{
-  auto drop_lang_item
-    = ctx->get_mappings ().lookup_lang_item (LangItem::Kind::DROP);
-
-  if (!drop_lang_item.has_value ())
-    return false;
-
-  DefId drop_id = drop_lang_item.value ();
-
-  auto candidates = Resolver::TypeBoundsProbe::Probe (ty);
-  for (auto &candidate : candidates)
-    {
-      Resolver::TraitReference *trait_ref = candidate.first;
-      if (trait_ref != nullptr && trait_ref->get_defid () == drop_id)
-       return true;
-    }
-
-  return false;
-}
-
 void
 CompilePatternCheckExpr::visit (HIR::PathInExpression &pattern)
 {
@@ -1377,7 +1356,7 @@ CompilePatternLet::visit (HIR::IdentifierPattern &pattern)
       drop_ty = ref_ty->get_base ();
     }
 
-  if (!type_has_drop_impl (ctx, drop_ty))
+  if (!CompileDrop::type_has_drop_impl (ctx, drop_ty))
     return;
 
   if (!pattern.has_subpattern () && !pattern.get_is_ref ())
-- 
2.54.0

Reply via email to