From: Arthur Cohen Date: Mon, 21 Aug 2023 13:52:06 +0000 (+0200) Subject: gccrs: forever-stack: Fix basic get logic X-Git-Tag: basepoints/gcc-15~1607 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=446ab9b265554b8e20ff386466f119fcf9e11093;p=thirdparty%2Fgcc.git gccrs: forever-stack: Fix basic get logic gcc/rust/ChangeLog: * resolve/rust-forever-stack.h: Improve resolve_path API. * resolve/rust-forever-stack.hxx: Likewise and fix implementation. --- diff --git a/gcc/rust/resolve/rust-forever-stack.h b/gcc/rust/resolve/rust-forever-stack.h index 7ee084919870..349d0971f616 100644 --- a/gcc/rust/resolve/rust-forever-stack.h +++ b/gcc/rust/resolve/rust-forever-stack.h @@ -473,7 +473,7 @@ public: * @return a valid option with the NodeId if the path is present in the * current map, an empty one otherwise. */ - tl::optional resolve_path (const AST::SimplePath &path); + template tl::optional resolve_path (const P &path); std::string as_debug_string (); @@ -550,18 +550,19 @@ private: /* Helper types and functions for `resolve_path` */ - using SegIterator = std::vector::const_iterator; + template + using SegIterator = typename std::vector::const_iterator; Node &find_closest_module (Node &starting_point); - tl::optional - find_starting_point (const std::vector &segments, - Node &starting_point); + template + tl::optional> + find_starting_point (const std::vector &segments, Node &starting_point); - tl::optional - resolve_segments (Node &starting_point, - const std::vector &segments, - SegIterator iterator); + template + tl::optional resolve_segments (Node &starting_point, + const std::vector &segments, + SegIterator iterator); }; } // namespace Resolver2_0 diff --git a/gcc/rust/resolve/rust-forever-stack.hxx b/gcc/rust/resolve/rust-forever-stack.hxx index 5acdf06c770c..806745eb9081 100644 --- a/gcc/rust/resolve/rust-forever-stack.hxx +++ b/gcc/rust/resolve/rust-forever-stack.hxx @@ -208,15 +208,8 @@ ForeverStack::update_cursor (Node &new_cursor) } template -tl::optional -ForeverStack::get (const Identifier &name) -{ - return tl::nullopt; -} - -template <> inline tl::optional -ForeverStack::get (const Identifier &name) +ForeverStack::get (const Identifier &name) { tl::optional resolved_node = tl::nullopt; @@ -226,9 +219,9 @@ ForeverStack::get (const Identifier &name) return candidate.map_or ( [&resolved_node] (NodeId found) { - // macro resolving does not need to care about various ribs - they are - // available from all contexts if defined in the current scope, or an - // outermore one. so if we do have a candidate, we can return it + // for most namespaces, we do not need to care about various ribs - they + // are available from all contexts if defined in the current scope, or + // an outermore one. so if we do have a candidate, we can return it // directly and stop iterating resolved_node = found; @@ -278,9 +271,9 @@ ForeverStack::find_closest_module (Node &starting_point) /* If a the given condition is met, emit an error about misused leading path * segments */ +template static inline bool -check_leading_kw_at_start (const AST::SimplePathSegment &segment, - bool condition) +check_leading_kw_at_start (const S &segment, bool condition) { if (condition) rust_error_at ( @@ -297,9 +290,10 @@ check_leading_kw_at_start (const AST::SimplePathSegment &segment, // `super` segment, we go back to the cursor's parent until we reach the // correct one or the root. template -tl::optional::const_iterator> -ForeverStack::find_starting_point ( - const std::vector &segments, Node &starting_point) +template +tl::optional::const_iterator> +ForeverStack::find_starting_point (const std::vector &segments, + Node &starting_point) { auto iterator = segments.begin (); @@ -357,10 +351,11 @@ ForeverStack::find_starting_point ( } template +template tl::optional::Node &> ForeverStack::resolve_segments ( - Node &starting_point, const std::vector &segments, - std::vector::const_iterator iterator) + Node &starting_point, const std::vector &segments, + typename std::vector::const_iterator iterator) { auto *current_node = &starting_point; for (; !is_last (iterator, segments); iterator++) @@ -407,9 +402,14 @@ ForeverStack::resolve_segments ( } template +template tl::optional -ForeverStack::resolve_path (const AST::SimplePath &path) +ForeverStack::resolve_path (const P &path) { + // if there's only one segment, we just use `get` + if (path.get_segments ().size () == 1) + return get (path.get_final_segment ().as_string ()); + auto starting_point = cursor (); auto &segments = path.get_segments ();