diff options
author | Owen Avery <powerboat9.gamer@gmail.com> | 2024-10-26 20:52:31 -0400 |
---|---|---|
committer | Arthur Cohen <arthur.cohen@embecosm.com> | 2025-03-21 12:32:55 +0100 |
commit | 2a44562d0b65d9ec13d00f7eba87fe77cb1c6af3 (patch) | |
tree | c983fa9d51b4f2bafed7085a8b82d8d43671638b /gcc/rust/resolve | |
parent | b1e6a3f76e10d7d9ee3574b7abd9d0281ea2d22d (diff) | |
download | gcc-2a44562d0b65d9ec13d00f7eba87fe77cb1c6af3.zip gcc-2a44562d0b65d9ec13d00f7eba87fe77cb1c6af3.tar.gz gcc-2a44562d0b65d9ec13d00f7eba87fe77cb1c6af3.tar.bz2 |
gccrs: Use name resolver 2.0 for module descendance checks
gcc/rust/ChangeLog:
* checks/errors/privacy/rust-privacy-reporter.cc:
Include rust-immutable-name-resolution-context.h.
(is_child_module): Use ForeverStack::is_module_descendant if name
resolution 2.0 is enabled.
* resolve/rust-forever-stack.h
(ForeverStack::is_module_descendant): Add.
(ForeverStack::dfs_node): Add.
* resolve/rust-forever-stack.hxx
(ForeverStack::dfs_rib): Use ForeverStack::dfs_node.
(ForeverStack::dfs_node): Add.
(ForeverStack::is_module_descendant): Add.
Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
Diffstat (limited to 'gcc/rust/resolve')
-rw-r--r-- | gcc/rust/resolve/rust-forever-stack.h | 10 | ||||
-rw-r--r-- | gcc/rust/resolve/rust-forever-stack.hxx | 41 |
2 files changed, 44 insertions, 7 deletions
diff --git a/gcc/rust/resolve/rust-forever-stack.h b/gcc/rust/resolve/rust-forever-stack.h index 8c5e207..2850925 100644 --- a/gcc/rust/resolve/rust-forever-stack.h +++ b/gcc/rust/resolve/rust-forever-stack.h @@ -521,6 +521,12 @@ public: std::string as_debug_string (); + /** + * Used to check if a module is a descendant of another module + * Intended for use in the privacy checker + */ + bool is_module_descendant (NodeId parent, NodeId child) const; + private: /** * A link between two Nodes in our trie data structure. This class represents @@ -635,6 +641,10 @@ private: tl::optional<Rib &> dfs_rib (Node &starting_point, NodeId to_find); tl::optional<const Rib &> dfs_rib (const Node &starting_point, NodeId to_find) const; + // FIXME: Documentation + tl::optional<Node &> dfs_node (Node &starting_point, NodeId to_find); + tl::optional<const Node &> dfs_node (const Node &starting_point, + NodeId to_find) const; }; } // namespace Resolver2_0 diff --git a/gcc/rust/resolve/rust-forever-stack.hxx b/gcc/rust/resolve/rust-forever-stack.hxx index 5a5a7c7..31f8ba4 100644 --- a/gcc/rust/resolve/rust-forever-stack.hxx +++ b/gcc/rust/resolve/rust-forever-stack.hxx @@ -626,12 +626,32 @@ template <Namespace N> tl::optional<Rib &> ForeverStack<N>::dfs_rib (ForeverStack<N>::Node &starting_point, NodeId to_find) { + return dfs_node (starting_point, to_find).map ([] (Node &x) -> Rib & { + return x.rib; + }); +} + +template <Namespace N> +tl::optional<const Rib &> +ForeverStack<N>::dfs_rib (const ForeverStack<N>::Node &starting_point, + NodeId to_find) const +{ + return dfs_node (starting_point, to_find).map ([] (Node &x) -> Rib & { + return x.rib; + }); +} + +template <Namespace N> +tl::optional<typename ForeverStack<N>::Node &> +ForeverStack<N>::dfs_node (ForeverStack<N>::Node &starting_point, + NodeId to_find) +{ if (starting_point.id == to_find) - return starting_point.rib; + return starting_point; for (auto &child : starting_point.children) { - auto candidate = dfs_rib (child.second, to_find); + auto candidate = dfs_node (child.second, to_find); if (candidate.has_value ()) return candidate; @@ -641,16 +661,16 @@ 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 +tl::optional<const typename ForeverStack<N>::Node &> +ForeverStack<N>::dfs_node (const ForeverStack<N>::Node &starting_point, + NodeId to_find) const { if (starting_point.id == to_find) - return starting_point.rib; + return starting_point; for (auto &child : starting_point.children) { - auto candidate = dfs_rib (child.second, to_find); + auto candidate = dfs_node (child.second, to_find); if (candidate.has_value ()) return candidate; @@ -737,6 +757,13 @@ ForeverStack<N>::as_debug_string () return stream.str (); } +template <Namespace N> +bool +ForeverStack<N>::is_module_descendant (NodeId parent, NodeId child) const +{ + return dfs_node (dfs_node (root, parent).value (), child).has_value (); +} + // FIXME: Can we add selftests? } // namespace Resolver2_0 |