]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
nr2.0: Start using newtype pattern for Usage and Declaration
authorArthur Cohen <arthur.cohen@embecosm.com>
Thu, 14 Sep 2023 15:38:06 +0000 (17:38 +0200)
committerP-E-P <32375388+P-E-P@users.noreply.github.com>
Tue, 26 Mar 2024 17:35:02 +0000 (17:35 +0000)
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.

gcc/rust/resolve/rust-late-name-resolver-2.0.cc
gcc/rust/resolve/rust-name-resolution-context.cc
gcc/rust/resolve/rust-name-resolution-context.h

index e5a4f23487124402253a49ee382c0d0a3bf02076..50034073edfea7427928afc00aeb36ea78a2d27e 100644 (file)
@@ -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
index 9fd8d52968a8d1c98eeedfd02b569c49531bca28..0340d28f127873c330106207ebb963524eabcb8e 100644 (file)
@@ -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
index e896ca0536086fa6a2799ec0102d4c63574320c9..8702900d0f3a538c2c8f7ac4e9095abf7e294efb 100644 (file)
@@ -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