public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Thomas Schwinge <tschwinge@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc/devel/rust/master] Refactor TraitResolver to not require a visitor
Date: Mon, 17 Oct 2022 12:34:10 +0000 (GMT)	[thread overview]
Message-ID: <20221017123410.8698A3858D28@sourceware.org> (raw)

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

commit e8c82076bc6aff3d8df4da228dfdb617ae7372e2
Author: Philip Herron <philip.herron@embecosm.com>
Date:   Fri Oct 14 14:00:16 2022 +0100

    Refactor TraitResolver to not require a visitor
    
    We used a visitor to dispatch the HIR::Item so that we could cast it directly to an HIR::Trait and then check for nullptr if it failed. This
    patch changes this to simply use our new enum item_type_kind to switch so
    we can directly static_cast.

Diff:
---
 gcc/rust/typecheck/rust-hir-trait-resolve.cc | 53 ++++++++++++----------------
 gcc/rust/typecheck/rust-hir-trait-resolve.h  | 10 ++----
 2 files changed, 26 insertions(+), 37 deletions(-)

diff --git a/gcc/rust/typecheck/rust-hir-trait-resolve.cc b/gcc/rust/typecheck/rust-hir-trait-resolve.cc
index 146fe26a762..cc23fe22d7f 100644
--- a/gcc/rust/typecheck/rust-hir-trait-resolve.cc
+++ b/gcc/rust/typecheck/rust-hir-trait-resolve.cc
@@ -91,33 +91,42 @@ TraitResolver::Lookup (HIR::TypePath &path)
   return resolver.lookup_path (path);
 }
 
-TraitResolver::TraitResolver ()
-  : TypeCheckBase (), resolved_trait_reference (nullptr)
-{}
+TraitResolver::TraitResolver () : TypeCheckBase () {}
 
-TraitReference *
-TraitResolver::resolve_path (HIR::TypePath &path)
+bool
+TraitResolver::resolve_path_to_trait (const HIR::TypePath &path,
+				      HIR::Trait **resolved) const
 {
   NodeId ref;
   if (!resolver->lookup_resolved_type (path.get_mappings ().get_nodeid (),
 				       &ref))
     {
       rust_error_at (path.get_locus (), "Failed to resolve path to node-id");
-      return &TraitReference::error_node ();
+      return false;
     }
 
   HirId hir_node = UNKNOWN_HIRID;
   if (!mappings->lookup_node_to_hir (ref, &hir_node))
     {
       rust_error_at (path.get_locus (), "Failed to resolve path to hir-id");
-      return &TraitReference::error_node ();
+      return false;
     }
 
   HIR::Item *resolved_item = mappings->lookup_hir_item (hir_node);
-
   rust_assert (resolved_item != nullptr);
-  resolved_item->accept_vis (*this);
-  rust_assert (resolved_trait_reference != nullptr);
+  rust_assert (resolved_item->get_item_kind () == HIR::Item::ItemKind::Trait);
+  *resolved = static_cast<HIR::Trait *> (resolved_item);
+
+  return true;
+}
+
+TraitReference *
+TraitResolver::resolve_path (HIR::TypePath &path)
+{
+  HIR::Trait *resolved_trait_reference;
+  bool ok = resolve_path_to_trait (path, &resolved_trait_reference);
+  if (!ok)
+    return &TraitReference::error_node ();
 
   return resolve_trait (resolved_trait_reference);
 }
@@ -237,26 +246,10 @@ TraitResolver::resolve_trait (HIR::Trait *trait_reference)
 TraitReference *
 TraitResolver::lookup_path (HIR::TypePath &path)
 {
-  NodeId ref;
-  if (!resolver->lookup_resolved_type (path.get_mappings ().get_nodeid (),
-				       &ref))
-    {
-      rust_error_at (path.get_locus (), "Failed to resolve path to node-id");
-      return &TraitReference::error_node ();
-    }
-
-  HirId hir_node = UNKNOWN_HIRID;
-  if (!mappings->lookup_node_to_hir (ref, &hir_node))
-    {
-      rust_error_at (path.get_locus (), "Failed to resolve path to hir-id");
-      return &TraitReference::error_node ();
-    }
-
-  HIR::Item *resolved_item = mappings->lookup_hir_item (hir_node);
-
-  rust_assert (resolved_item != nullptr);
-  resolved_item->accept_vis (*this);
-  rust_assert (resolved_trait_reference != nullptr);
+  HIR::Trait *resolved_trait_reference;
+  bool ok = resolve_path_to_trait (path, &resolved_trait_reference);
+  if (!ok)
+    return &TraitReference::error_node ();
 
   TraitReference *tref = &TraitReference::error_node ();
   if (context->lookup_trait_reference (
diff --git a/gcc/rust/typecheck/rust-hir-trait-resolve.h b/gcc/rust/typecheck/rust-hir-trait-resolve.h
index c4aaf42b141..468f42985bd 100644
--- a/gcc/rust/typecheck/rust-hir-trait-resolve.h
+++ b/gcc/rust/typecheck/rust-hir-trait-resolve.h
@@ -55,10 +55,8 @@ private:
   std::vector<TyTy::SubstitutionParamMapping> substitutions;
 };
 
-class TraitResolver : public TypeCheckBase, private HIR::HIRFullVisitorBase
+class TraitResolver : public TypeCheckBase
 {
-  using HIR::HIRFullVisitorBase::visit;
-
 public:
   static TraitReference *Resolve (HIR::TypePath &path);
 
@@ -75,10 +73,8 @@ private:
 
   TraitReference *lookup_path (HIR::TypePath &path);
 
-  HIR::Trait *resolved_trait_reference;
-
-public:
-  void visit (HIR::Trait &trait) override { resolved_trait_reference = &trait; }
+  bool resolve_path_to_trait (const HIR::TypePath &path,
+			      HIR::Trait **resolved) const;
 };
 
 } // namespace Resolver

                 reply	other threads:[~2022-10-17 12:34 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221017123410.8698A3858D28@sourceware.org \
    --to=tschwinge@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).