public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] resolver: Refactor Rib class in a source file
@ 2022-06-08 12:36 Thomas Schwinge
  0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2022-06-08 12:36 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:3001bb64cc25e9174cf69ad5e12df17ad45ef6b8

commit 3001bb64cc25e9174cf69ad5e12df17ad45ef6b8
Author: Arthur Cohen <arthur.cohen@embecosm.com>
Date:   Fri Apr 22 16:28:26 2022 +0200

    resolver: Refactor Rib class in a source file

Diff:
---
 gcc/rust/Make-lang.in                  |   1 +
 gcc/rust/backend/rust-compile-base.cc  |  52 +++++++--------
 gcc/rust/resolve/rust-name-resolver.cc | 108 +++++++++++++++++++++++++++++++
 gcc/rust/resolve/rust-name-resolver.h  | 115 +++------------------------------
 4 files changed, 144 insertions(+), 132 deletions(-)

diff --git a/gcc/rust/Make-lang.in b/gcc/rust/Make-lang.in
index d25f403fbfb..436d9c3d6c4 100644
--- a/gcc/rust/Make-lang.in
+++ b/gcc/rust/Make-lang.in
@@ -85,6 +85,7 @@ GRS_OBJS = \
     rust/rust-ast-lower.o \
     rust/rust-ast-lower-base.o \
     rust/rust-ast-lower-pattern.o \
+    rust/rust-name-resolver.o \
     rust/rust-ast-resolve.o \
     rust/rust-ast-resolve-pattern.o \
     rust/rust-ast-resolve-expr.o \
diff --git a/gcc/rust/backend/rust-compile-base.cc b/gcc/rust/backend/rust-compile-base.cc
index 84afa1e17be..1ee7cd59beb 100644
--- a/gcc/rust/backend/rust-compile-base.cc
+++ b/gcc/rust/backend/rust-compile-base.cc
@@ -306,33 +306,33 @@ HIRCompileBase::compile_locals_for_block (Context *ctx, Resolver::Rib &rib,
 					  tree fndecl)
 {
   std::vector<Bvariable *> locals;
-  rib.iterate_decls ([&] (NodeId n, Location) mutable -> bool {
-    Resolver::Definition d;
-    bool ok = ctx->get_resolver ()->lookup_definition (n, &d);
-    rust_assert (ok);
-
-    HIR::Stmt *decl = nullptr;
-    ok = ctx->get_mappings ()->resolve_nodeid_to_stmt (d.parent, &decl);
-    rust_assert (ok);
-
-    // if its a function we extract this out side of this fn context
-    // and it is not a local to this function
-    bool is_item = ctx->get_mappings ()->lookup_hir_item (
-		     decl->get_mappings ().get_crate_num (),
-		     decl->get_mappings ().get_hirid ())
-		   != nullptr;
-    if (is_item)
-      {
-	HIR::Item *item = static_cast<HIR::Item *> (decl);
-	CompileItem::compile (item, ctx);
-	return true;
-      }
-
-    Bvariable *compiled = CompileVarDecl::compile (fndecl, decl, ctx);
-    locals.push_back (compiled);
+  for (auto it : rib.get_declarations ())
+    {
+      auto node_id = it.first;
+
+      Resolver::Definition d;
+      bool ok = ctx->get_resolver ()->lookup_definition (node_id, &d);
+      rust_assert (ok);
+
+      HIR::Stmt *decl = nullptr;
+      ok = ctx->get_mappings ()->resolve_nodeid_to_stmt (d.parent, &decl);
+      rust_assert (ok);
+
+      // if its a function we extract this out side of this fn context
+      // and it is not a local to this function
+      bool is_item = ctx->get_mappings ()->lookup_hir_item (
+		       decl->get_mappings ().get_crate_num (),
+		       decl->get_mappings ().get_hirid ())
+		     != nullptr;
+      if (is_item)
+	{
+	  HIR::Item *item = static_cast<HIR::Item *> (decl);
+	  CompileItem::compile (item, ctx);
+	}
 
-    return true;
-  });
+      Bvariable *compiled = CompileVarDecl::compile (fndecl, decl, ctx);
+      locals.push_back (compiled);
+    };
 
   return locals;
 }
diff --git a/gcc/rust/resolve/rust-name-resolver.cc b/gcc/rust/resolve/rust-name-resolver.cc
new file mode 100644
index 00000000000..a80d64f1787
--- /dev/null
+++ b/gcc/rust/resolve/rust-name-resolver.cc
@@ -0,0 +1,108 @@
+// Copyright (C) 2020-2022 Free Software Foundation, Inc.
+
+// This file is part of GCC.
+
+// GCC is free software; you can redistribute it and/or modify it under
+// the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 3, or (at your option) any later
+// version.
+
+// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+// WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+// for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with GCC; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+#include "rust-name-resolver.h"
+
+namespace Rust {
+namespace Resolver {
+
+Rib::Rib (CrateNum crateNum, NodeId node_id)
+  : crate_num (crateNum), node_id (node_id),
+    mappings (Analysis::Mappings::get ())
+{}
+
+void
+Rib::insert_name (
+  const CanonicalPath &path, NodeId id, Location locus, bool shadow,
+  std::function<void (const CanonicalPath &, NodeId, Location)> dup_cb)
+{
+  auto it = path_mappings.find (path);
+  bool path_already_exists = it != path_mappings.end ();
+  if (path_already_exists && !shadow)
+    {
+      const auto &decl = decls_within_rib.find (it->second);
+      if (decl != decls_within_rib.end ())
+	dup_cb (path, it->second, decl->second);
+      else
+	dup_cb (path, it->second, locus);
+
+      return;
+    }
+
+  path_mappings[path] = id;
+  reverse_path_mappings.insert (std::pair<NodeId, CanonicalPath> (id, path));
+  decls_within_rib.insert (std::pair<NodeId, Location> (id, locus));
+  references[id] = {};
+}
+
+bool
+Rib::lookup_name (const CanonicalPath &ident, NodeId *id)
+{
+  auto it = path_mappings.find (ident);
+  if (it == path_mappings.end ())
+    return false;
+
+  *id = it->second;
+  return true;
+}
+
+void
+Rib::clear_name (const CanonicalPath &ident, NodeId id)
+{
+  auto ii = path_mappings.find (ident);
+  if (ii != path_mappings.end ())
+    path_mappings.erase (ii);
+
+  auto ij = reverse_path_mappings.find (id);
+  if (ij != reverse_path_mappings.end ())
+    reverse_path_mappings.erase (ij);
+
+  auto ik = decls_within_rib.find (id);
+  if (ik != decls_within_rib.end ())
+    decls_within_rib.erase (ik);
+}
+
+void
+Rib::append_reference_for_def (NodeId def, NodeId ref)
+{
+  references[def].insert (ref);
+}
+
+bool
+Rib::have_references_for_node (NodeId def) const
+{
+  auto it = references.find (def);
+  if (it == references.end ())
+    return false;
+
+  return !it->second.empty ();
+}
+
+bool
+Rib::decl_was_declared_here (NodeId def) const
+{
+  for (auto &it : decls_within_rib)
+    {
+      if (it.first == def)
+	return true;
+    }
+  return false;
+}
+
+} // namespace Resolver
+} // namespace Rust
diff --git a/gcc/rust/resolve/rust-name-resolver.h b/gcc/rust/resolve/rust-name-resolver.h
index 208448036d9..19ca5a9fd15 100644
--- a/gcc/rust/resolve/rust-name-resolver.h
+++ b/gcc/rust/resolve/rust-name-resolver.h
@@ -32,120 +32,23 @@ class Rib
 public:
   // Rust uses local_def_ids assigned by def_collector on the AST
   // lets use NodeId instead
-  Rib (CrateNum crateNum, NodeId node_id)
-    : crate_num (crateNum), node_id (node_id),
-      mappings (Analysis::Mappings::get ())
-  {}
-
-  ~Rib () {}
+  Rib (CrateNum crateNum, NodeId node_id);
 
   // this takes the relative paths of items within a compilation unit for lookup
   void insert_name (
     const CanonicalPath &path, NodeId id, Location locus, bool shadow,
-    std::function<void (const CanonicalPath &, NodeId, Location)> dup_cb)
-  {
-    auto it = path_mappings.find (path);
-    bool path_already_exists = it != path_mappings.end ();
-    if (path_already_exists && !shadow)
-      {
-	const auto &decl = decls_within_rib.find (it->second);
-	if (decl != decls_within_rib.end ())
-	  dup_cb (path, it->second, decl->second);
-	else
-	  dup_cb (path, it->second, locus);
-
-	return;
-      }
-
-    path_mappings[path] = id;
-    reverse_path_mappings.insert (std::pair<NodeId, CanonicalPath> (id, path));
-    decls_within_rib.insert (std::pair<NodeId, Location> (id, locus));
-    references[id] = {};
-  }
-
-  bool lookup_name (const CanonicalPath &ident, NodeId *id)
-  {
-    auto it = path_mappings.find (ident);
-    if (it == path_mappings.end ())
-      return false;
-
-    *id = it->second;
-    return true;
-  }
+    std::function<void (const CanonicalPath &, NodeId, Location)> dup_cb);
 
-  bool lookup_canonical_path (const NodeId &id, CanonicalPath *ident)
-  {
-    auto it = reverse_path_mappings.find (id);
-    if (it == reverse_path_mappings.end ())
-      return false;
-
-    *ident = it->second;
-    return true;
-  }
-
-  void clear_name (const CanonicalPath &ident, NodeId id)
-  {
-    auto ii = path_mappings.find (ident);
-    if (ii != path_mappings.end ())
-      path_mappings.erase (ii);
-
-    auto ij = reverse_path_mappings.find (id);
-    if (ij != reverse_path_mappings.end ())
-      reverse_path_mappings.erase (ij);
-
-    auto ik = decls_within_rib.find (id);
-    if (ik != decls_within_rib.end ())
-      decls_within_rib.erase (ik);
-  }
+  bool lookup_canonical_path (const NodeId &id, CanonicalPath *ident);
+  bool lookup_name (const CanonicalPath &ident, NodeId *id);
+  void clear_name (const CanonicalPath &ident, NodeId id);
+  void append_reference_for_def (NodeId def, NodeId ref);
+  bool have_references_for_node (NodeId def) const;
+  bool decl_was_declared_here (NodeId def) const;
 
   CrateNum get_crate_num () const { return crate_num; }
   NodeId get_node_id () const { return node_id; }
-
-  void iterate_decls (std::function<bool (NodeId, Location)> cb)
-  {
-    for (auto it : decls_within_rib)
-      {
-	if (!cb (it.first, it.second))
-	  return;
-      }
-  }
-
-  void iterate_references_for_def (NodeId def, std::function<bool (NodeId)> cb)
-  {
-    auto it = references.find (def);
-    if (it == references.end ())
-      return;
-
-    for (auto ref : it->second)
-      {
-	if (!cb (ref))
-	  return;
-      }
-  }
-
-  void append_reference_for_def (NodeId def, NodeId ref)
-  {
-    references[def].insert (ref);
-  }
-
-  bool have_references_for_node (NodeId def) const
-  {
-    auto it = references.find (def);
-    if (it == references.end ())
-      return false;
-
-    return !it->second.empty ();
-  }
-
-  bool decl_was_declared_here (NodeId def) const
-  {
-    for (auto &it : decls_within_rib)
-      {
-	if (it.first == def)
-	  return true;
-      }
-    return false;
-  }
+  std::map<NodeId, Location> &get_declarations () { return decls_within_rib; }
 
 private:
   CrateNum crate_num;


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-06-08 12:36 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-08 12:36 [gcc/devel/rust/master] resolver: Refactor Rib class in a source file Thomas Schwinge

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).