From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1643) id 53BD2381ECB6; Wed, 8 Jun 2022 12:40:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 53BD2381ECB6 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Thomas Schwinge To: gcc-cvs@gcc.gnu.org Subject: [gcc/devel/rust/master] ast: resolve: Add SimplePath path resolver X-Act-Checkin: gcc X-Git-Author: Arthur Cohen X-Git-Refname: refs/heads/devel/rust/master X-Git-Oldrev: 821c7947bfd6787fdaaba8dca2f9468661666dcc X-Git-Newrev: 06c5f54ccc1b94e0e7025478d3f36ee2eb334bde Message-Id: <20220608124025.53BD2381ECB6@sourceware.org> Date: Wed, 8 Jun 2022 12:40:25 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 08 Jun 2022 12:40:25 -0000 https://gcc.gnu.org/g:06c5f54ccc1b94e0e7025478d3f36ee2eb334bde commit 06c5f54ccc1b94e0e7025478d3f36ee2eb334bde Author: Arthur Cohen Date: Tue Apr 26 10:23:45 2022 +0200 ast: resolve: Add SimplePath path resolver Diff: --- gcc/rust/resolve/rust-ast-resolve-path.cc | 93 +++++++++++++++++++++++++++++++ gcc/rust/resolve/rust-ast-resolve-path.h | 8 ++- 2 files changed, 100 insertions(+), 1 deletion(-) diff --git a/gcc/rust/resolve/rust-ast-resolve-path.cc b/gcc/rust/resolve/rust-ast-resolve-path.cc index 7aec4f8a70e..3e4bb3352de 100644 --- a/gcc/rust/resolve/rust-ast-resolve-path.cc +++ b/gcc/rust/resolve/rust-ast-resolve-path.cc @@ -37,6 +37,13 @@ ResolvePath::go (AST::QualifiedPathInExpression *expr, NodeId parent) resolver.resolve_path (expr); } +void +ResolvePath::go (AST::SimplePath *expr, NodeId parent) +{ + ResolvePath resolver (parent); + resolver.resolve_path (expr); +} + void ResolvePath::resolve_path (AST::PathInExpression *expr) { @@ -272,5 +279,91 @@ ResolvePath::resolve_segments (CanonicalPath prefix, size_t offs, } } +void +ResolvePath::resolve_path (AST::SimplePath *expr) +{ + // resolve root segment first then apply segments in turn + auto &segs = expr->get_segments (); + auto &root_segment = segs.at (0); + auto &root_ident_seg = root_segment.get_segment_name (); + + /** + * TODO: We need to handle functions and types later on for `use` statements. + * So we will also need to check the type scope + * + * bool segment_is_type = false; + * bool segment_is_func = false; + */ + CanonicalPath root_seg_path + = CanonicalPath::new_seg (root_segment.get_node_id (), root_ident_seg); + + // name scope first + if (resolver->get_name_scope ().lookup (root_seg_path, &resolved_node)) + { + resolver->insert_resolved_name (root_segment.get_node_id (), + resolved_node); + resolver->insert_new_definition (root_segment.get_node_id (), + Definition{expr->get_node_id (), + parent}); + } + else + { + rust_error_at (expr->get_locus (), + "Cannot find path %<%s%> in this scope", + root_segment.as_string ().c_str ()); + return; + } + + bool is_single_segment = segs.size () == 1; + if (is_single_segment) + { + // if (segment_is_type) + // resolver->insert_resolved_type (expr->get_node_id (), resolved_node); + + resolver->insert_resolved_name (expr->get_node_id (), resolved_node); + resolver->insert_new_definition (expr->get_node_id (), + Definition{expr->get_node_id (), + parent}); + return; + } + + resolve_simple_path_segments (root_seg_path, 1, expr->get_segments (), + expr->get_node_id (), expr->get_locus ()); +} + +void +ResolvePath::resolve_simple_path_segments ( + CanonicalPath prefix, size_t offs, + const std::vector &segs, NodeId expr_node_id, + Location expr_locus) +{ + /** + * TODO: We also need to handle types and functions here + */ + + CanonicalPath path = prefix; + for (const auto &seg : segs) + { + auto s = ResolveSimplePathSegmentToCanonicalPath::resolve (seg); + path = path.append (s); + + resolved_node = UNKNOWN_NODEID; + + if (resolver->get_name_scope ().lookup (path, &resolved_node)) + { + resolver->insert_resolved_name (seg.get_node_id (), resolved_node); + resolver->insert_new_definition (seg.get_node_id (), + Definition{expr_node_id, parent}); + } + } + + if (resolved_node != UNKNOWN_NODEID) + { + resolver->insert_resolved_name (expr_node_id, resolved_node); + resolver->insert_new_definition (expr_node_id, + Definition{expr_node_id, parent}); + } +} + } // namespace Resolver } // namespace Rust diff --git a/gcc/rust/resolve/rust-ast-resolve-path.h b/gcc/rust/resolve/rust-ast-resolve-path.h index 60793d331ab..cbfe967fdcc 100644 --- a/gcc/rust/resolve/rust-ast-resolve-path.h +++ b/gcc/rust/resolve/rust-ast-resolve-path.h @@ -31,6 +31,7 @@ class ResolvePath : public ResolverBase public: static void go (AST::PathInExpression *expr, NodeId parent); static void go (AST::QualifiedPathInExpression *expr, NodeId parent); + static void go (AST::SimplePath *expr, NodeId parent); private: ResolvePath (NodeId parent) : ResolverBase (parent) {} @@ -42,12 +43,17 @@ private: void resolve_segments (CanonicalPath prefix, size_t offs, std::vector &segs, NodeId expr_node_id, Location expr_locus); + + void + resolve_simple_path_segments (CanonicalPath prefix, size_t offs, + const std::vector &segs, + NodeId expr_node_id, Location expr_locus); }; class ResolveSimplePathSegmentToCanonicalPath { public: - static CanonicalPath resolve (AST::SimplePathSegment &seg) + static CanonicalPath resolve (const AST::SimplePathSegment &seg) { // FIXME: Since this is so simple, maybe it can simply be a tiny function? return CanonicalPath::new_seg (seg.get_node_id (), seg.get_segment_name ());