https://gcc.gnu.org/g:81f2edc5d1fcc82196e2831fe1222d7d04dcb2b2

commit 81f2edc5d1fcc82196e2831fe1222d7d04dcb2b2
Author: Arthur Cohen <arthur.co...@embecosm.com>
Date:   Thu Sep 14 17:38:06 2023 +0200

    nr2.0: Start using newtype pattern for Usage and Declaration
    
    gcc/rust/ChangeLog:
    
            * resolve/rust-name-resolution-context.cc 
(NameResolutionContext::map_usage):
            Use newtype pattern.
            (NameResolutionContext::lookup): Likewise.
            * resolve/rust-name-resolution-context.h (class Usage): New class.
            (class Definition): Likewise.
            * resolve/rust-late-name-resolver-2.0.cc (Late::visit): Create 
instances
            of Usage and Definition.

Diff:
---
 gcc/rust/resolve/rust-late-name-resolver-2.0.cc  |  4 ++--
 gcc/rust/resolve/rust-name-resolution-context.cc |  6 +++---
 gcc/rust/resolve/rust-name-resolution-context.h  | 27 ++++++++++++++++++++++--
 3 files changed, 30 insertions(+), 7 deletions(-)

diff --git a/gcc/rust/resolve/rust-late-name-resolver-2.0.cc 
b/gcc/rust/resolve/rust-late-name-resolver-2.0.cc
index e5a4f2348712..50034073edfe 100644
--- a/gcc/rust/resolve/rust-late-name-resolver-2.0.cc
+++ b/gcc/rust/resolve/rust-late-name-resolver-2.0.cc
@@ -142,7 +142,7 @@ Late::visit (AST::IdentifierExpr &expr)
       return;
     }
 
-  ctx.map_usage (expr.get_node_id (), *resolved);
+  ctx.map_usage (Usage (expr.get_node_id ()), Definition (*resolved));
 
   // in the old resolver, resolutions are kept in the resolver, not the 
mappings
   // :/ how do we deal with that?
@@ -173,7 +173,7 @@ Late::visit (AST::TypePath &type)
 
   auto resolved = ctx.types.get (type.get_segments ().back ()->as_string ());
 
-  ctx.map_usage (type.get_node_id (), *resolved);
+  ctx.map_usage (Usage (type.get_node_id ()), Definition (*resolved));
 }
 
 } // namespace Resolver2_0
diff --git a/gcc/rust/resolve/rust-name-resolution-context.cc 
b/gcc/rust/resolve/rust-name-resolution-context.cc
index 9fd8d52968a8..0340d28f1278 100644
--- a/gcc/rust/resolve/rust-name-resolution-context.cc
+++ b/gcc/rust/resolve/rust-name-resolution-context.cc
@@ -46,7 +46,7 @@ NameResolutionContext::insert (Identifier name, NodeId id, 
Namespace ns)
 }
 
 void
-NameResolutionContext::map_usage (NodeId usage, NodeId definition)
+NameResolutionContext::map_usage (Usage usage, Definition definition)
 {
   auto inserted = resolved_nodes.emplace (usage, definition).second;
 
@@ -57,12 +57,12 @@ NameResolutionContext::map_usage (NodeId usage, NodeId 
definition)
 tl::optional<NodeId>
 NameResolutionContext::lookup (NodeId usage)
 {
-  auto it = resolved_nodes.find (usage);
+  auto it = resolved_nodes.find (Usage (usage));
 
   if (it == resolved_nodes.end ())
     return tl::nullopt;
 
-  return it->second;
+  return it->second.id;
 }
 
 void
diff --git a/gcc/rust/resolve/rust-name-resolution-context.h 
b/gcc/rust/resolve/rust-name-resolution-context.h
index e896ca053608..8702900d0f3a 100644
--- a/gcc/rust/resolve/rust-name-resolution-context.h
+++ b/gcc/rust/resolve/rust-name-resolution-context.h
@@ -133,6 +133,28 @@ change?
 correct
 */
 
+// FIXME: Documentation
+class Usage
+{
+public:
+  explicit Usage (NodeId id) : id (id) {}
+
+  // TODO: move to name-resolution-ctx.cc
+  // storing it as a key in a map
+  bool operator< (const Usage other) const { return other.id < id; }
+
+  NodeId id;
+};
+
+// FIXME: Documentation
+class Definition
+{
+public:
+  explicit Definition (NodeId id) : id (id) {}
+
+  NodeId id;
+};
+
 // Now our resolver, which keeps track of all the `ForeverStack`s we could want
 class NameResolutionContext
 {
@@ -182,12 +204,13 @@ public:
 
   // TODO: Rename
   // TODO: Use newtype pattern for Usage and Definition
-  void map_usage (NodeId usage, NodeId definition);
+  void map_usage (Usage usage, Definition definition);
+
   tl::optional<NodeId> lookup (NodeId usage);
 
 private:
   /* Map of "usage" nodes which have been resolved to a "definition" node */
-  std::map<NodeId, NodeId> resolved_nodes;
+  std::map<Usage, Definition> resolved_nodes;
 };
 
 } // namespace Resolver2_0

Reply via email to