public inbox for gcc-rust@gcc.gnu.org
 help / color / mirror / Atom feed
From: arthur.cohen@embecosm.com
To: gcc-patches@gcc.gnu.org
Cc: gcc-rust@gcc.gnu.org, Arthur Cohen <arthur.cohen@embecosm.com>
Subject: [COMMITTED 012/101] gccrs: foreverstack: Add `to_canonical_path` method
Date: Tue, 30 Jan 2024 13:06:28 +0100	[thread overview]
Message-ID: <20240130121026.807464-15-arthur.cohen@embecosm.com> (raw)
In-Reply-To: <20240130121026.807464-2-arthur.cohen@embecosm.com>

From: Arthur Cohen <arthur.cohen@embecosm.com>

gcc/rust/ChangeLog:

	* resolve/rust-forever-stack.h: New method.
	* resolve/rust-forever-stack.hxx: Likewise.
---
 gcc/rust/resolve/rust-forever-stack.h   | 25 ++++++-
 gcc/rust/resolve/rust-forever-stack.hxx | 90 ++++++++++++++++++++++++-
 2 files changed, 110 insertions(+), 5 deletions(-)

diff --git a/gcc/rust/resolve/rust-forever-stack.h b/gcc/rust/resolve/rust-forever-stack.h
index ec469a9b3fa..37277ddb3ad 100644
--- a/gcc/rust/resolve/rust-forever-stack.h
+++ b/gcc/rust/resolve/rust-forever-stack.h
@@ -396,7 +396,10 @@ template <Namespace N> class ForeverStack
 {
 public:
   ForeverStack ()
-    : root (Node (Rib (Rib::Kind::Normal))), cursor_reference (root)
+    // FIXME: Is that valid? Do we use the root? If yes, we should give the
+    // crate's node id to ForeverStack's constructor
+    : root (Node (Rib (Rib::Kind::Normal), UNKNOWN_NODEID)),
+      cursor_reference (root)
   {
     rust_assert (root.is_root ());
     rust_assert (root.is_leaf ());
@@ -478,6 +481,12 @@ public:
   template <typename S>
   tl::optional<NodeId> resolve_path (const std::vector<S> &segments);
 
+  // FIXME: Documentation
+  tl::optional<Resolver::CanonicalPath> to_canonical_path (NodeId id);
+
+  // FIXME: Documentation
+  tl::optional<Rib &> to_rib (NodeId rib_id);
+
   std::string as_debug_string ();
 
 private:
@@ -509,8 +518,10 @@ private:
   class Node
   {
   public:
-    Node (Rib rib) : rib (rib) {}
-    Node (Rib rib, Node &parent) : rib (rib), parent (parent) {}
+    Node (Rib rib, NodeId id) : rib (rib), id (id) {}
+    Node (Rib rib, NodeId id, Node &parent)
+      : rib (rib), id (id), parent (parent)
+    {}
 
     bool is_root () const;
     bool is_leaf () const;
@@ -520,6 +531,8 @@ private:
     Rib rib; // this is the "value" of the node - the data it keeps.
     std::map<Link, Node, LinkCmp> children; // all the other nodes it links to
 
+    NodeId id; // The node id of the Node's scope
+
     tl::optional<Node &> parent; // `None` only if the node is a root
   };
 
@@ -566,6 +579,12 @@ private:
   tl::optional<Node &> resolve_segments (Node &starting_point,
 					 const std::vector<S> &segments,
 					 SegIterator<S> iterator);
+
+  /* Helper functions for forward resolution (to_canonical_path, to_rib...) */
+
+  // FIXME: Documentation
+  tl::optional<std::pair<Node &, std::string>> dfs (Node &starting_point,
+						    NodeId to_find);
 };
 
 } // namespace Resolver2_0
diff --git a/gcc/rust/resolve/rust-forever-stack.hxx b/gcc/rust/resolve/rust-forever-stack.hxx
index 642135cda85..4e06da235bf 100644
--- a/gcc/rust/resolve/rust-forever-stack.hxx
+++ b/gcc/rust/resolve/rust-forever-stack.hxx
@@ -66,8 +66,8 @@ ForeverStack<N>::push_inner (Rib rib, Link link)
   // the iterator and a boolean. If the value already exists, the iterator
   // points to it. Otherwise, it points to the newly emplaced value, so we can
   // just update our cursor().
-  auto emplace
-    = cursor ().children.emplace (std::make_pair (link, Node (rib, cursor ())));
+  auto emplace = cursor ().children.emplace (
+    std::make_pair (link, Node (rib, link.id, cursor ())));
 
   auto it = emplace.first;
   auto existed = !emplace.second;
@@ -451,6 +451,92 @@ ForeverStack<N>::resolve_path (const std::vector<S> &segments)
     });
 }
 
+template <Namespace N>
+tl::optional<std::pair<typename ForeverStack<N>::Node &, std::string>>
+ForeverStack<N>::dfs (ForeverStack<N>::Node &starting_point, NodeId to_find)
+{
+  auto &values = starting_point.rib.get_values ();
+
+  for (auto &kv : values)
+    if (kv.second == to_find)
+      return {{starting_point, kv.first}};
+
+  for (auto &child : starting_point.children)
+    {
+      auto candidate = dfs (child.second, to_find);
+
+      if (candidate.has_value ())
+	return candidate;
+    }
+
+  return tl::nullopt;
+}
+
+template <Namespace N>
+tl::optional<Resolver::CanonicalPath>
+ForeverStack<N>::to_canonical_path (NodeId id)
+{
+  // find the id in the current forever stack, starting from the root,
+  // performing either a BFS or DFS once the Node containing the ID is found, go
+  // back up to the root (parent().parent().parent()...) accumulate link
+  // segments reverse them that's your canonical path
+
+  return dfs (root, id).map ([this, id] (std::pair<Node &, std::string> tuple) {
+    auto containing_node = tuple.first;
+    auto name = tuple.second;
+
+    auto segments = std::vector<Resolver::CanonicalPath> ();
+
+    reverse_iter (containing_node, [&segments] (Node &current) {
+      if (current.is_root ())
+	return KeepGoing::No;
+
+      auto children = current.parent.value ().children;
+      const Link *outer_link = nullptr;
+
+      for (auto &kv : children)
+	{
+	  auto &link = kv.first;
+	  auto &child = kv.second;
+
+	  if (link.id == child.id)
+	    {
+	      outer_link = &link;
+	      break;
+	    }
+	}
+
+      rust_assert (outer_link);
+
+      outer_link->path.map ([&segments, outer_link] (Identifier path) {
+	segments.emplace (segments.begin (),
+			  Resolver::CanonicalPath::new_seg (outer_link->id,
+							    path.as_string ()));
+      });
+
+      return KeepGoing::Yes;
+    });
+
+    auto path = Resolver::CanonicalPath::create_empty ();
+    for (const auto &segment : segments)
+      path = path.append (segment);
+
+    // Finally, append the name
+    path = path.append (Resolver::CanonicalPath::new_seg (id, name));
+    rust_debug ("[ARTHUR] found path: %s. Size: %lu", path.get ().c_str (),
+		segments.size ());
+
+    return path;
+  });
+}
+
+template <Namespace N>
+tl::optional<Rib &>
+ForeverStack<N>::to_rib (NodeId rib_id)
+{
+  return tl::nullopt;
+}
+
 template <Namespace N>
 void
 ForeverStack<N>::stream_rib (std::stringstream &stream, const Rib &rib,
-- 
2.42.1


  parent reply	other threads:[~2024-01-30 12:10 UTC|newest]

Thread overview: 102+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-30 12:06 [PATCHSET] Update Rust frontend January 2024 arthur.cohen
2024-01-30 12:06 ` [COMMITTED 001/101] gccrs: Add visibility to trait item arthur.cohen
2024-01-30 12:06 ` [COMMITTED 002/101] gccrs: Add a test to highlight public trait type parsing arthur.cohen
2024-01-30 12:06 ` [COMMITTED 003/101] gccrs: Fix error emission for self pointers arthur.cohen
2024-01-30 12:06 ` [COMMITTED 004/101] gccrs: Report self parameter parsing error kind arthur.cohen
2024-01-30 12:06 ` [COMMITTED 005/101] gccrs: Add new test for parsing errors on self pointers arthur.cohen
2024-01-30 12:06 ` [COMMITTED 006/101] gccrs: ast: Change *Path nodes API arthur.cohen
2024-01-30 12:06 ` [COMMITTED 007/101] gccrs: rib: Add Namespace enum arthur.cohen
2024-01-30 12:06 ` [COMMITTED 008/101] gccrs: forever-stack: Fix basic get logic arthur.cohen
2024-01-30 12:06 ` [COMMITTED 009/101] gccrs: foreverstack: Specialize `get` for Namespace::Labels arthur.cohen
2024-01-30 12:06 ` [COMMITTED 010/101] gccrs: forever stack: Fix resolve_path signature arthur.cohen
2024-01-30 12:06 ` [COMMITTED 011/101] gccrs: forever stack: Improve resolve_path implementation arthur.cohen
2024-01-30 12:06 ` arthur.cohen [this message]
2024-01-30 12:06 ` [COMMITTED 013/101] gccrs: foreverstack: Add `to_rib` method arthur.cohen
2024-01-30 12:06 ` [COMMITTED 014/101] gccrs: resolve: Format if properly arthur.cohen
2024-01-30 12:06 ` [COMMITTED 015/101] gccrs: forever stack: Remove development debug info arthur.cohen
2024-01-30 12:06 ` [COMMITTED 016/101] gccrs: Reject auto traits with generic parameters arthur.cohen
2024-01-30 12:06 ` [COMMITTED 017/101] gccrs: Add regression test for generic auto traits arthur.cohen
2024-01-30 12:06 ` [COMMITTED 018/101] gccrs: Reject auto traits with super trait arthur.cohen
2024-01-30 12:06 ` [COMMITTED 019/101] gccrs: Add a regression test for super trait on auto trait arthur.cohen
2024-01-30 12:06 ` [COMMITTED 020/101] gccrs: Add check for associated items on auto traits arthur.cohen
2024-01-30 12:06 ` [COMMITTED 021/101] gccrs: Emit an error on variadic non extern functions arthur.cohen
2024-01-30 12:06 ` [COMMITTED 022/101] gccrs: Add a test regular variadic functions errors arthur.cohen
2024-01-30 12:06 ` [COMMITTED 023/101] gccrs: Add ast validation check on union variant number arthur.cohen
2024-01-30 12:06 ` [COMMITTED 024/101] gccrs: Replace TOK suffix with KW arthur.cohen
2024-01-30 12:06 ` [COMMITTED 025/101] gccrs: Add edition separation for keywords arthur.cohen
2024-01-30 12:06 ` [COMMITTED 026/101] gccrs: Treat underscore as a keyword arthur.cohen
2024-01-30 12:06 ` [COMMITTED 027/101] gccrs: Add await keyword arthur.cohen
2024-01-30 12:06 ` [COMMITTED 028/101] gccrs: Replace some keyword raw values arthur.cohen
2024-01-30 12:06 ` [COMMITTED 029/101] gccrs: Add a list of weak keyword arthur.cohen
2024-01-30 12:06 ` [COMMITTED 030/101] gccrs: Replace some weak keyword raw value with constexpr arthur.cohen
2024-01-30 12:06 ` [COMMITTED 031/101] gccrs: Introduce a proper keyword list arthur.cohen
2024-01-30 12:06 ` [COMMITTED 032/101] gccrs: Added support to Parse ASYNC function arthur.cohen
2024-01-30 12:06 ` [COMMITTED 033/101] gccrs: ctx: Add Labels ForeverStack to the resolver arthur.cohen
2024-01-30 12:06 ` [COMMITTED 034/101] gccrs: nr2.0: Add base for late name resolution arthur.cohen
2024-01-30 12:06 ` [COMMITTED 035/101] gccrs: toplevel: Use DefaultResolver for Function arthur.cohen
2024-01-30 12:06 ` [COMMITTED 036/101] gccrs: nr2.0: Store mappings in NameResolutionContext arthur.cohen
2024-01-30 12:06 ` [COMMITTED 037/101] gccrs: late: Start setting up builtin types arthur.cohen
2024-01-30 12:06 ` [COMMITTED 038/101] gccrs: late: Start storing mappings properly in the resolver arthur.cohen
2024-01-30 12:06 ` [COMMITTED 039/101] gccrs: early: Resolve paths properly arthur.cohen
2024-01-30 12:06 ` [COMMITTED 040/101] gccrs: toplevel: Add comment about running the collector twice arthur.cohen
2024-01-30 12:06 ` [COMMITTED 041/101] gccrs: ast: Add NodeId to UseTree base class arthur.cohen
2024-01-30 12:06 ` [COMMITTED 042/101] gccrs: early: Move `use` declaration resolving to TopLevel arthur.cohen
2024-01-30 12:06 ` [COMMITTED 043/101] gccrs: toplevel: Resolve `use` declarations arthur.cohen
2024-01-30 12:07 ` [COMMITTED 044/101] gccrs: Create base class for TupleStructItems and TuplePatternItems arthur.cohen
2024-01-30 12:07 ` [COMMITTED 045/101] gccrs: Add unsafety member to modules arthur.cohen
2024-01-30 12:07 ` [COMMITTED 046/101] gccrs: Parse module safety arthur.cohen
2024-01-30 12:07 ` [COMMITTED 047/101] gccrs: Emit an error on unsafe modules arthur.cohen
2024-01-30 12:07 ` [COMMITTED 048/101] gccrs: Add a regression test for unsafe module validation arthur.cohen
2024-01-30 12:07 ` [COMMITTED 049/101] gccrs: Remove backend dependancy on resolution rib information arthur.cohen
2024-01-30 12:07 ` [COMMITTED 050/101] gccrs: Remove class AST::InherentImplItem arthur.cohen
2024-01-30 12:07 ` [COMMITTED 051/101] gccrs: Split async and const function qualifiers arthur.cohen
2024-01-30 12:07 ` [COMMITTED 052/101] gccrs: Allow const and async specifiers in functions arthur.cohen
2024-01-30 12:07 ` [COMMITTED 053/101] gccrs: Add async const function ast validation pass arthur.cohen
2024-01-30 12:07 ` [COMMITTED 054/101] gccrs: Add a regression test for async const functions arthur.cohen
2024-01-30 12:07 ` [COMMITTED 055/101] gccrs: Add AST validation check for const in trait arthur.cohen
2024-01-30 12:07 ` [COMMITTED 056/101] gccrs: Add regression test for const fn " arthur.cohen
2024-01-30 12:07 ` [COMMITTED 057/101] gccrs: Make feature gate visitor inherit from default one arthur.cohen
2024-01-30 12:07 ` [COMMITTED 058/101] gccrs: Change the attribute checker visitor to " arthur.cohen
2024-01-30 12:07 ` [COMMITTED 059/101] gccrs: Make early name resolver inherit from " arthur.cohen
2024-01-30 12:07 ` [COMMITTED 060/101] gccrs: Add multiple regression test in name resolution arthur.cohen
2024-01-30 12:07 ` [COMMITTED 061/101] gccrs: Add execution test for name resolution 2.0 arthur.cohen
2024-01-30 12:07 ` [COMMITTED 062/101] gccrs: Make function bodies truly optional arthur.cohen
2024-01-30 12:07 ` [COMMITTED 063/101] gccrs: Add validation for functions without body arthur.cohen
2024-01-30 12:07 ` [COMMITTED 064/101] gccrs: Add a regression test for function body check arthur.cohen
2024-01-30 12:07 ` [COMMITTED 065/101] gccrs: Generate error for const trait functions arthur.cohen
2024-01-30 12:07 ` [COMMITTED 066/101] gccrs: Renamed `WIN64` to `WIN_64` arthur.cohen
2024-01-30 12:07 ` [COMMITTED 067/101] gccrs: Allow enabling lang_items and no_core features arthur.cohen
2024-01-30 12:07 ` [COMMITTED 068/101] gccrs: Make default resolver inherit from default visitor arthur.cohen
2024-01-30 12:07 ` [COMMITTED 069/101] gccrs: Make expand visitor " arthur.cohen
2024-01-30 12:07 ` [COMMITTED 070/101] gccrs: Change cfg stripper to use " arthur.cohen
2024-01-30 12:07 ` [COMMITTED 071/101] gccrs: refactor builtins initialization and attributes arthur.cohen
2024-01-30 12:07 ` [COMMITTED 072/101] gccrs: HIR: add missing getters arthur.cohen
2024-01-30 12:07 ` [COMMITTED 073/101] gccrs: TyTy: Fix missed nodiscard arthur.cohen
2024-01-30 12:07 ` [COMMITTED 074/101] gccrs: BIR: " arthur.cohen
2024-01-30 12:07 ` [COMMITTED 075/101] gccrs: TyTy: refactor to new API arthur.cohen
2024-01-30 12:07 ` [COMMITTED 076/101] gccrs: TyTy: Common interface for fucntion-like types arthur.cohen
2024-01-30 12:07 ` [COMMITTED 077/101] gccrs: TyTy: SubstitutionRef cast specialization arthur.cohen
2024-01-30 12:07 ` [COMMITTED 078/101] gccrs: BIR: Cleanup arthur.cohen
2024-01-30 12:07 ` [COMMITTED 079/101] gccrs: split rust-mangle.cc into two files arthur.cohen
2024-01-30 12:07 ` [COMMITTED 080/101] gccrs: Handle `async` qualifier inside trait arthur.cohen
2024-01-30 12:07 ` [COMMITTED 081/101] gccrs: Generate error for `async` trait fucntions arthur.cohen
2024-01-30 12:07 ` [COMMITTED 082/101] gccrs: ast: Fix lifetime type parsing arthur.cohen
2024-01-30 12:07 ` [COMMITTED 083/101] gccrs: ast: Unify explicitly and implicitly elided lifettimes arthur.cohen
2024-01-30 12:07 ` [COMMITTED 084/101] gccrs: ast: Full lifetime elision handling arthur.cohen
2024-01-30 12:07 ` [COMMITTED 085/101] gccrs: ast: Infer static lifetime for const and static items arthur.cohen
2024-01-30 12:07 ` [COMMITTED 086/101] gccrs: ast: Lower 'for' lifetimes arthur.cohen
2024-01-30 12:07 ` [COMMITTED 087/101] gccrs: TyTy: Refactor FnType deprecated API arthur.cohen
2024-01-30 12:07 ` [COMMITTED 088/101] gccrs: Handle newlines during string parsing while lexing arthur.cohen
2024-01-30 12:07 ` [COMMITTED 089/101] gccrs: Handle `async` functions in traits arthur.cohen
2024-01-30 12:07 ` [COMMITTED 090/101] gccrs: Fix inconsistent formatting arthur.cohen
2024-01-30 12:07 ` [COMMITTED 091/101] gccrs: Handle `async` keyword for regular implementations arthur.cohen
2024-01-30 12:07 ` [COMMITTED 092/101] gccrs: Add improved error when a field is redefined in a struct constructor arthur.cohen
2024-01-30 12:07 ` [COMMITTED 093/101] gccrs: Unify storage of associated items in SingleASTNode arthur.cohen
2024-01-30 12:07 ` [COMMITTED 094/101] gccrs: Added newline to get more readable lexdump arthur.cohen
2024-01-30 12:07 ` [COMMITTED 095/101] gccrs: Test: fix missing lifetime in a test arthur.cohen
2024-01-30 12:07 ` [COMMITTED 096/101] gccrs: AST: Fix for lifetime parsing arthur.cohen
2024-01-30 12:07 ` [COMMITTED 097/101] gccrs: AST: Fix for lifetime lowering arthur.cohen
2024-01-30 12:07 ` [COMMITTED 098/101] gccrs: Test: check implemented for lifetime handling arthur.cohen
2024-01-30 12:07 ` [COMMITTED 099/101] gccrs: Add improved error when no fields in initializer arthur.cohen
2024-01-30 12:07 ` [COMMITTED 100/101] gccrs: Remove TraitImplItem arthur.cohen
2024-01-30 12:07 ` [COMMITTED 101/101] gccrs: Fix output line ending patterns arthur.cohen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240130121026.807464-15-arthur.cohen@embecosm.com \
    --to=arthur.cohen@embecosm.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=gcc-rust@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).