public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] Add new interface to mappings class crate_num_to_nodeid and node_is_crate
@ 2022-07-15 11:37 Thomas Schwinge
  0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2022-07-15 11:37 UTC (permalink / raw)
  To: gcc-cvs

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

commit e3f135ef0beb51b77cae7c6af40c5a1c4dfe7722
Author: Philip Herron <philip.herron@embecosm.com>
Date:   Fri Jul 8 15:08:08 2022 +0100

    Add new interface to mappings class crate_num_to_nodeid and node_is_crate
    
    In order to load and resolve extern crates we need to support detection
    of node_id is a crate in order to support paths outside of the current
    crate.

Diff:
---
 gcc/rust/resolve/rust-ast-resolve-path.cc |  3 +-
 gcc/rust/resolve/rust-ast-resolve-type.cc |  3 +-
 gcc/rust/util/rust-hir-map.cc             | 64 +++++++++++++++++++++++++++++++
 gcc/rust/util/rust-hir-map.h              | 31 ++++-----------
 4 files changed, 75 insertions(+), 26 deletions(-)

diff --git a/gcc/rust/resolve/rust-ast-resolve-path.cc b/gcc/rust/resolve/rust-ast-resolve-path.cc
index a03bfb435b5..27f32aa37ff 100644
--- a/gcc/rust/resolve/rust-ast-resolve-path.cc
+++ b/gcc/rust/resolve/rust-ast-resolve-path.cc
@@ -199,7 +199,8 @@ ResolvePath::resolve_path (AST::PathInExpression *expr)
       bool did_resolve_segment = resolved_node_id != UNKNOWN_NODEID;
       if (did_resolve_segment)
 	{
-	  if (mappings->node_is_module (resolved_node_id))
+	  if (mappings->node_is_module (resolved_node_id)
+	      || mappings->node_is_crate (resolved_node_id))
 	    {
 	      module_scope_id = resolved_node_id;
 	    }
diff --git a/gcc/rust/resolve/rust-ast-resolve-type.cc b/gcc/rust/resolve/rust-ast-resolve-type.cc
index 8e85889ffd1..04183a4bc61 100644
--- a/gcc/rust/resolve/rust-ast-resolve-type.cc
+++ b/gcc/rust/resolve/rust-ast-resolve-type.cc
@@ -220,7 +220,8 @@ ResolveRelativeTypePath::go (AST::TypePath &path, NodeId &resolved_node_id)
       bool did_resolve_segment = resolved_node_id != UNKNOWN_NODEID;
       if (did_resolve_segment)
 	{
-	  if (mappings->node_is_module (resolved_node_id))
+	  if (mappings->node_is_module (resolved_node_id)
+	      || mappings->node_is_crate (resolved_node_id))
 	    {
 	      module_scope_id = resolved_node_id;
 	    }
diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc
index 353b7cb16ed..7d2371702c5 100644
--- a/gcc/rust/util/rust-hir-map.cc
+++ b/gcc/rust/util/rust-hir-map.cc
@@ -130,6 +130,70 @@ Mappings::get_current_crate () const
   return currentCrateNum;
 }
 
+bool
+Mappings::get_crate_name (CrateNum crate_num, std::string &name) const
+{
+  auto it = crate_names.find (crate_num);
+  if (it == crate_names.end ())
+    return false;
+
+  name.assign (it->second);
+  return true;
+}
+
+void
+Mappings::set_crate_name (CrateNum crate_num, const std::string &name)
+{
+  crate_names[crate_num] = name;
+}
+
+std::string
+Mappings::get_current_crate_name () const
+{
+  std::string name;
+  bool ok = get_crate_name (get_current_crate (), name);
+  rust_assert (ok);
+  return name;
+}
+
+bool
+Mappings::lookup_crate_name (const std::string &crate_name,
+			     CrateNum &resolved_crate_num) const
+{
+  for (const auto &it : crate_names)
+    {
+      if (it.second.compare (crate_name) == 0)
+	{
+	  resolved_crate_num = it.first;
+	  return true;
+	}
+    }
+  return false;
+}
+
+bool
+Mappings::crate_num_to_nodeid (const CrateNum &crate_num, NodeId &node_id) const
+{
+  auto it = ast_crate_mappings.find (crate_num);
+  if (it == ast_crate_mappings.end ())
+    return false;
+
+  node_id = it->second->get_node_id ();
+  return true;
+}
+
+bool
+Mappings::node_is_crate (NodeId node_id) const
+{
+  for (const auto &it : ast_crate_mappings)
+    {
+      NodeId crate_node_id = it.second->get_node_id ();
+      if (crate_node_id == node_id)
+	return true;
+    }
+  return false;
+}
+
 NodeId
 Mappings::get_next_node_id ()
 {
diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h
index 804242e99bb..c8cebefcd04 100644
--- a/gcc/rust/util/rust-hir-map.h
+++ b/gcc/rust/util/rust-hir-map.h
@@ -77,30 +77,13 @@ public:
   CrateNum get_next_crate_num (const std::string &name);
   void set_current_crate (CrateNum crateNum);
   CrateNum get_current_crate () const;
-
-  bool get_crate_name (CrateNum crate_num, std::string &name) const
-  {
-    auto it = crate_names.find (crate_num);
-    if (it == crate_names.end ())
-      return false;
-
-    name.assign (it->second);
-    return true;
-  }
-
-  // set crate name mid-compilation
-  void set_crate_name (CrateNum crate_num, const std::string &name)
-  {
-    crate_names[crate_num] = name;
-  }
-
-  std::string get_current_crate_name () const
-  {
-    std::string name;
-    bool ok = get_crate_name (get_current_crate (), name);
-    rust_assert (ok);
-    return name;
-  }
+  bool get_crate_name (CrateNum crate_num, std::string &name) const;
+  void set_crate_name (CrateNum crate_num, const std::string &name);
+  std::string get_current_crate_name () const;
+  bool lookup_crate_name (const std::string &crate_name,
+			  CrateNum &resolved_crate_num) const;
+  bool crate_num_to_nodeid (const CrateNum &crate_num, NodeId &node_id) const;
+  bool node_is_crate (NodeId node_id) const;
 
   NodeId get_next_node_id ();
   HirId get_next_hir_id () { return get_next_hir_id (get_current_crate ()); }


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

only message in thread, other threads:[~2022-07-15 11:37 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-15 11:37 [gcc/devel/rust/master] Add new interface to mappings class crate_num_to_nodeid and node_is_crate 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).