diff options
author | Owen Avery <powerboat9.gamer@gmail.com> | 2024-10-21 18:35:30 -0400 |
---|---|---|
committer | Arthur Cohen <arthur.cohen@embecosm.com> | 2025-03-19 15:32:18 +0100 |
commit | e2aa4557b8fae5d19259c90f83b9b193440f7b4e (patch) | |
tree | a9784113a572a3a2b0604bac594bc6611fe2e464 /gcc/rust/resolve/rust-forever-stack.hxx | |
parent | 2110efcc2a62284efe196d68b614f7aa963c4611 (diff) | |
download | gcc-e2aa4557b8fae5d19259c90f83b9b193440f7b4e.zip gcc-e2aa4557b8fae5d19259c90f83b9b193440f7b4e.tar.gz gcc-e2aa4557b8fae5d19259c90f83b9b193440f7b4e.tar.bz2 |
gccrs: Make const references to ForeverStack more useful
gcc/rust/ChangeLog:
* resolve/rust-forever-stack.h
(ForeverStack::to_canonical_path): Make const.
(ForeverStack::to_rib): Add const overload.
(ForeverStack::reverse_iter): Add const overloads.
(ForeverStack::ConstDfsResult): Add.
(ForeverStack::dfs): Add const overload.
(ForeverStack::dfs_rib): Likewise.
* resolve/rust-forever-stack.hxx
(ForeverStack::reverse_iter): Add const overloads.
(ForeverStack::dfs): Add const overload.
(ForeverStack::to_canonical_path): Make const.
(ForeverStack::dfs_rib): Likewise.
(ForeverStack::to_rib): Add const overload.
Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
Diffstat (limited to 'gcc/rust/resolve/rust-forever-stack.hxx')
-rw-r--r-- | gcc/rust/resolve/rust-forever-stack.hxx | 91 |
1 files changed, 88 insertions, 3 deletions
diff --git a/gcc/rust/resolve/rust-forever-stack.hxx b/gcc/rust/resolve/rust-forever-stack.hxx index 4a8b6b5..5a5a7c7 100644 --- a/gcc/rust/resolve/rust-forever-stack.hxx +++ b/gcc/rust/resolve/rust-forever-stack.hxx @@ -194,6 +194,14 @@ ForeverStack<N>::reverse_iter (std::function<KeepGoing (Node &)> lambda) template <Namespace N> void +ForeverStack<N>::reverse_iter ( + std::function<KeepGoing (const Node &)> lambda) const +{ + return reverse_iter (cursor (), lambda); +} + +template <Namespace N> +void ForeverStack<N>::reverse_iter (Node &start, std::function<KeepGoing (Node &)> lambda) { @@ -213,6 +221,26 @@ ForeverStack<N>::reverse_iter (Node &start, } template <Namespace N> +void +ForeverStack<N>::reverse_iter ( + const Node &start, std::function<KeepGoing (const Node &)> lambda) const +{ + auto *tmp = &start; + + while (true) + { + auto keep_going = lambda (*tmp); + if (keep_going == KeepGoing::No) + return; + + if (tmp->is_root ()) + return; + + tmp = &tmp->parent.value (); + } +} + +template <Namespace N> typename ForeverStack<N>::Node & ForeverStack<N>::cursor () { @@ -508,21 +536,52 @@ ForeverStack<N>::dfs (ForeverStack<N>::Node &starting_point, NodeId to_find) } template <Namespace N> +tl::optional<typename ForeverStack<N>::ConstDfsResult> +ForeverStack<N>::dfs (const ForeverStack<N>::Node &starting_point, + NodeId to_find) const +{ + auto values = starting_point.rib.get_values (); + + for (auto &kv : values) + { + for (auto id : kv.second.ids_shadowable) + if (id == to_find) + return {{starting_point, kv.first}}; + for (auto id : kv.second.ids_non_shadowable) + if (id == to_find) + return {{starting_point, kv.first}}; + for (auto id : kv.second.ids_globbed) + if (id == 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) +ForeverStack<N>::to_canonical_path (NodeId id) const { // 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] (DfsResult tuple) { + return dfs (root, id).map ([this, id] (ConstDfsResult tuple) { auto containing_node = tuple.first; auto name = tuple.second; auto segments = std::vector<Resolver::CanonicalPath> (); - reverse_iter (containing_node, [&segments] (Node ¤t) { + reverse_iter (containing_node, [&segments] (const Node ¤t) { if (current.is_root ()) return KeepGoing::No; @@ -582,6 +641,25 @@ ForeverStack<N>::dfs_rib (ForeverStack<N>::Node &starting_point, NodeId to_find) } template <Namespace N> +tl::optional<const Rib &> +ForeverStack<N>::dfs_rib (const ForeverStack<N>::Node &starting_point, + NodeId to_find) const +{ + if (starting_point.id == to_find) + return starting_point.rib; + + for (auto &child : starting_point.children) + { + auto candidate = dfs_rib (child.second, to_find); + + if (candidate.has_value ()) + return candidate; + } + + return tl::nullopt; +} + +template <Namespace N> tl::optional<Rib &> ForeverStack<N>::to_rib (NodeId rib_id) { @@ -589,6 +667,13 @@ ForeverStack<N>::to_rib (NodeId rib_id) } template <Namespace N> +tl::optional<const Rib &> +ForeverStack<N>::to_rib (NodeId rib_id) const +{ + return dfs_rib (root, rib_id); +} + +template <Namespace N> void ForeverStack<N>::stream_rib (std::stringstream &stream, const Rib &rib, const std::string &next, |