public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] ast: resolve: Add SimplePath path resolver
@ 2022-06-08 12:40 Thomas Schwinge
0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2022-06-08 12:40 UTC (permalink / raw)
To: gcc-cvs
https://gcc.gnu.org/g:06c5f54ccc1b94e0e7025478d3f36ee2eb334bde
commit 06c5f54ccc1b94e0e7025478d3f36ee2eb334bde
Author: Arthur Cohen <arthur.cohen@embecosm.com>
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<AST::SimplePathSegment> &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<AST::PathExprSegment> &segs,
NodeId expr_node_id, Location expr_locus);
+
+ void
+ resolve_simple_path_segments (CanonicalPath prefix, size_t offs,
+ const std::vector<AST::SimplePathSegment> &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 ());
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2022-06-08 12:40 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-08 12:40 [gcc/devel/rust/master] ast: resolve: Add SimplePath path resolver Thomas Schwinge
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).