https://gcc.gnu.org/g:77a6a631b4b2421d0a7b7b8753b9d677ec611104

commit 77a6a631b4b2421d0a7b7b8753b9d677ec611104
Author: Arthur Cohen <arthur.co...@embecosm.com>
Date:   Thu Aug 24 17:51:11 2023 +0200

    nr2.0: Add lookup of resolved nodes.
    
    gcc/rust/ChangeLog:
    
            * resolve/rust-name-resolution-context.cc 
(NameResolutionContext::lookup):
            Add lookup function.
            * resolve/rust-name-resolution-context.h: Include mappings and 
optional.

Diff:
---
 gcc/rust/resolve/rust-name-resolution-context.cc | 13 +++++++++++++
 gcc/rust/resolve/rust-name-resolution-context.h  |  3 +++
 2 files changed, 16 insertions(+)

diff --git a/gcc/rust/resolve/rust-name-resolution-context.cc 
b/gcc/rust/resolve/rust-name-resolution-context.cc
index f35db7e925e5..9fd8d52968a8 100644
--- a/gcc/rust/resolve/rust-name-resolution-context.cc
+++ b/gcc/rust/resolve/rust-name-resolution-context.cc
@@ -17,6 +17,8 @@
 // <http://www.gnu.org/licenses/>.
 
 #include "rust-name-resolution-context.h"
+#include "optional.h"
+#include "rust-mapping-common.h"
 
 namespace Rust {
 namespace Resolver2_0 {
@@ -52,6 +54,17 @@ NameResolutionContext::map_usage (NodeId usage, NodeId 
definition)
   rust_assert (inserted);
 }
 
+tl::optional<NodeId>
+NameResolutionContext::lookup (NodeId usage)
+{
+  auto it = resolved_nodes.find (usage);
+
+  if (it == resolved_nodes.end ())
+    return tl::nullopt;
+
+  return it->second;
+}
+
 void
 NameResolutionContext::scoped (Rib rib, NodeId id,
                               std::function<void (void)> lambda,
diff --git a/gcc/rust/resolve/rust-name-resolution-context.h 
b/gcc/rust/resolve/rust-name-resolution-context.h
index bc3c4bb5ddb7..e896ca053608 100644
--- a/gcc/rust/resolve/rust-name-resolution-context.h
+++ b/gcc/rust/resolve/rust-name-resolution-context.h
@@ -19,6 +19,7 @@
 #ifndef RUST_NAME_RESOLVER_2_0_H
 #define RUST_NAME_RESOLVER_2_0_H
 
+#include "optional.h"
 #include "rust-forever-stack.h"
 #include "rust-hir-map.h"
 
@@ -180,7 +181,9 @@ public:
   Analysis::Mappings &mappings;
 
   // TODO: Rename
+  // TODO: Use newtype pattern for Usage and Definition
   void map_usage (NodeId usage, NodeId definition);
+  tl::optional<NodeId> lookup (NodeId usage);
 
 private:
   /* Map of "usage" nodes which have been resolved to a "definition" node */

Reply via email to