aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/resolve
diff options
context:
space:
mode:
authorArthur Cohen <arthur.cohen@embecosm.com>2024-12-26 21:46:03 +0000
committerCohenArthur <arthur.cohen@embecosm.com>2025-01-16 14:00:31 +0000
commit432859bf783a7dc9fb81f267c5159c60435973cb (patch)
treef0581fcf770e63924f3094074b4e2266cfc3fc9e /gcc/rust/resolve
parent955c4f7e587c082f0d364e4305c78b35822bd6ab (diff)
downloadgcc-432859bf783a7dc9fb81f267c5159c60435973cb.zip
gcc-432859bf783a7dc9fb81f267c5159c60435973cb.tar.gz
gcc-432859bf783a7dc9fb81f267c5159c60435973cb.tar.bz2
ast: Refactor how lang item paths are handled.
Lang item typepaths were not handled properly, and required a complete overhaul. All old classes that concerned lang item paths are now modified to use a simpler version of `AST::LangItemPath`, which has been removed. TypePath segments can now be lang items, as this is requied for having generic lang item paths such as PhantomData<T>. gcc/rust/ChangeLog: * ast/rust-path.h: Rework how lang item paths are represented. * ast/rust-path.cc: Likewise. * ast/rust-item.h: Likewise. * ast/rust-ast.cc: Likewise. * ast/rust-ast-collector.cc: Adapt to new lang item path system. * ast/rust-ast-collector.h: Likewise. * ast/rust-ast-visitor.cc (DefaultASTVisitor::visit): Likewise. * ast/rust-ast-visitor.h: Likewise. * expand/rust-derive-copy.cc: Likewise. * expand/rust-derive.h: Likewise. * hir/rust-ast-lower-base.cc (ASTLoweringBase::visit): Likewise. * hir/rust-ast-lower-base.h: Likewise. * hir/rust-ast-lower-type.cc (ASTLowerTypePath::translate): Likewise. (ASTLowerTypePath::visit): Likewise. * hir/rust-ast-lower-type.h: Likewise. * resolve/rust-ast-resolve-base.cc (ResolverBase::visit): Likewise. * resolve/rust-ast-resolve-base.h: Likewise. * resolve/rust-ast-resolve-item.cc (ResolveItem::visit): Likewise. * resolve/rust-ast-resolve-type.h: Likewise. * resolve/rust-ast-resolve-type.cc (ResolveRelativeTypePath::go): Likewise. * resolve/rust-late-name-resolver-2.0.cc (Late::visit): Likewise. * resolve/rust-late-name-resolver-2.0.h: Likewise. * hir/tree/rust-hir-path.cc (TypePathSegment::TypePathSegment): Likewise. (TypePathSegmentGeneric::TypePathSegmentGeneric): Likewise. * hir/tree/rust-hir-path.h: Likewise. * typecheck/rust-hir-type-check-type.cc (TypeCheckType::resolve_root_path): Likewise. * ast/rust-ast-builder.cc: Likewise. * ast/rust-ast-builder.h: Likewise.
Diffstat (limited to 'gcc/rust/resolve')
-rw-r--r--gcc/rust/resolve/rust-ast-resolve-base.cc8
-rw-r--r--gcc/rust/resolve/rust-ast-resolve-base.h2
-rw-r--r--gcc/rust/resolve/rust-ast-resolve-item.cc27
-rw-r--r--gcc/rust/resolve/rust-ast-resolve-type.cc79
-rw-r--r--gcc/rust/resolve/rust-ast-resolve-type.h31
-rw-r--r--gcc/rust/resolve/rust-late-name-resolver-2.0.cc19
-rw-r--r--gcc/rust/resolve/rust-late-name-resolver-2.0.h1
7 files changed, 52 insertions, 115 deletions
diff --git a/gcc/rust/resolve/rust-ast-resolve-base.cc b/gcc/rust/resolve/rust-ast-resolve-base.cc
index 74b2756..9cc980a 100644
--- a/gcc/rust/resolve/rust-ast-resolve-base.cc
+++ b/gcc/rust/resolve/rust-ast-resolve-base.cc
@@ -72,14 +72,6 @@ ResolverBase::visit (AST::ConstGenericParam &)
{}
void
-ResolverBase::visit (AST::RegularPath &)
-{}
-
-void
-ResolverBase::visit (AST::LangItemPath &)
-{}
-
-void
ResolverBase::visit (AST::PathInExpression &)
{}
diff --git a/gcc/rust/resolve/rust-ast-resolve-base.h b/gcc/rust/resolve/rust-ast-resolve-base.h
index bc3e048..7f01d50 100644
--- a/gcc/rust/resolve/rust-ast-resolve-base.h
+++ b/gcc/rust/resolve/rust-ast-resolve-base.h
@@ -40,8 +40,6 @@ public:
void visit (AST::Lifetime &);
void visit (AST::LifetimeParam &);
void visit (AST::ConstGenericParam &);
- void visit (AST::RegularPath &);
- void visit (AST::LangItemPath &);
void visit (AST::PathInExpression &);
void visit (AST::TypePathSegment &);
void visit (AST::TypePathSegmentGeneric &);
diff --git a/gcc/rust/resolve/rust-ast-resolve-item.cc b/gcc/rust/resolve/rust-ast-resolve-item.cc
index 619efb0..ca09c2e 100644
--- a/gcc/rust/resolve/rust-ast-resolve-item.cc
+++ b/gcc/rust/resolve/rust-ast-resolve-item.cc
@@ -682,28 +682,15 @@ ResolveItem::visit (AST::TraitImpl &impl_block)
// setup paths
CanonicalPath canonical_trait_type = CanonicalPath::create_empty ();
- if (impl_block.get_trait_path ().get_path_kind ()
- == AST::Path::Kind::LangItem)
- {
- auto &lang_item
- = static_cast<AST::LangItemPath &> (impl_block.get_trait_path ());
- canonical_trait_type
- = CanonicalPath::new_seg (lang_item.get_node_id (),
- LangItem::ToString (
- lang_item.get_lang_item_kind ()));
- }
- else
+ ok = ResolveTypeToCanonicalPath::go (impl_block.get_trait_path (),
+ canonical_trait_type);
+ if (!ok)
{
- ok = ResolveTypeToCanonicalPath::go (impl_block.get_trait_path_type (),
- canonical_trait_type);
- if (!ok)
- {
- resolver->get_name_scope ().pop ();
- resolver->get_type_scope ().pop ();
- resolver->get_label_scope ().pop ();
- return;
- }
+ resolver->get_name_scope ().pop ();
+ resolver->get_type_scope ().pop ();
+ resolver->get_label_scope ().pop ();
+ return;
}
rust_debug ("AST::TraitImpl resolve trait type: {%s}",
diff --git a/gcc/rust/resolve/rust-ast-resolve-type.cc b/gcc/rust/resolve/rust-ast-resolve-type.cc
index a4878a2..6cd8571 100644
--- a/gcc/rust/resolve/rust-ast-resolve-type.cc
+++ b/gcc/rust/resolve/rust-ast-resolve-type.cc
@@ -20,6 +20,7 @@
#include "rust-ast-resolve-expr.h"
#include "rust-canonical-path.h"
#include "rust-type.h"
+#include "rust-hir-map.h"
namespace Rust {
namespace Resolver {
@@ -99,45 +100,57 @@ ResolveRelativeTypePath::go (AST::TypePath &path, NodeId &resolved_node_id)
for (size_t i = 0; i < path.get_segments ().size (); i++)
{
auto &segment = path.get_segments ().at (i);
- const AST::PathIdentSegment &ident_seg = segment->get_ident_segment ();
bool is_first_segment = i == 0;
- resolved_node_id = UNKNOWN_NODEID;
+ NodeId crate_scope_id = resolver->peek_crate_module_scope ();
+ auto ident_string = segment->is_lang_item ()
+ ? LangItem::PrettyString (segment->get_lang_item ())
+ : segment->get_ident_segment ().as_string ();
- bool in_middle_of_path = i > 0;
- if (in_middle_of_path && segment->is_lower_self_seg ())
- {
- rust_error_at (segment->get_locus (), ErrorCode::E0433,
- "failed to resolve: %<%s%> in paths can only be used "
- "in start position",
- segment->as_string ().c_str ());
- return false;
- }
+ resolved_node_id = UNKNOWN_NODEID;
- NodeId crate_scope_id = resolver->peek_crate_module_scope ();
- if (segment->is_crate_path_seg ())
+ if (segment->is_lang_item ())
{
- // what is the current crate scope node id?
- module_scope_id = crate_scope_id;
- previous_resolved_node_id = module_scope_id;
- resolver->insert_resolved_name (segment->get_node_id (),
- module_scope_id);
-
- continue;
+ resolved_node_id = Analysis::Mappings::get ().get_lang_item_node (
+ segment->get_lang_item ());
+ previous_resolved_node_id = resolved_node_id;
}
- else if (segment->is_super_path_seg ())
+ else
{
- if (module_scope_id == crate_scope_id)
+ bool in_middle_of_path = i > 0;
+ if (in_middle_of_path && segment->is_lower_self_seg ())
{
- rust_error_at (segment->get_locus (),
- "cannot use super at the crate scope");
+ rust_error_at (segment->get_locus (), ErrorCode::E0433,
+ "failed to resolve: %qs in paths can only be used "
+ "in start position",
+ segment->as_string ().c_str ());
return false;
}
- module_scope_id = resolver->peek_parent_module_scope ();
- previous_resolved_node_id = module_scope_id;
- resolver->insert_resolved_name (segment->get_node_id (),
- module_scope_id);
- continue;
+ if (segment->is_crate_path_seg ())
+ {
+ // what is the current crate scope node id?
+ module_scope_id = crate_scope_id;
+ previous_resolved_node_id = module_scope_id;
+ resolver->insert_resolved_name (segment->get_node_id (),
+ module_scope_id);
+
+ continue;
+ }
+ else if (segment->is_super_path_seg ())
+ {
+ if (module_scope_id == crate_scope_id)
+ {
+ rust_error_at (segment->get_locus (),
+ "cannot use super at the crate scope");
+ return false;
+ }
+
+ module_scope_id = resolver->peek_parent_module_scope ();
+ previous_resolved_node_id = module_scope_id;
+ resolver->insert_resolved_name (segment->get_node_id (),
+ module_scope_id);
+ continue;
+ }
}
switch (segment->get_type ())
@@ -177,8 +190,7 @@ ResolveRelativeTypePath::go (AST::TypePath &path, NodeId &resolved_node_id)
// name scope first
NodeId resolved_node = UNKNOWN_NODEID;
const CanonicalPath path
- = CanonicalPath::new_seg (segment->get_node_id (),
- ident_seg.as_string ());
+ = CanonicalPath::new_seg (segment->get_node_id (), ident_string);
if (resolver->get_type_scope ().lookup (path, &resolved_node))
{
resolver->insert_resolved_type (segment->get_node_id (),
@@ -191,7 +203,7 @@ ResolveRelativeTypePath::go (AST::TypePath &path, NodeId &resolved_node_id)
resolved_node);
resolved_node_id = resolved_node;
}
- else if (segment->is_lower_self_seg ())
+ else if (!segment->is_lang_item () && segment->is_lower_self_seg ())
{
// what is the current crate scope node id?
module_scope_id = crate_scope_id;
@@ -207,8 +219,7 @@ ResolveRelativeTypePath::go (AST::TypePath &path, NodeId &resolved_node_id)
&& previous_resolved_node_id == module_scope_id)
{
tl::optional<CanonicalPath &> resolved_child
- = mappings.lookup_module_child (module_scope_id,
- ident_seg.as_string ());
+ = mappings.lookup_module_child (module_scope_id, ident_string);
if (resolved_child.has_value ())
{
NodeId resolved_node = resolved_child->get_node_id ();
diff --git a/gcc/rust/resolve/rust-ast-resolve-type.h b/gcc/rust/resolve/rust-ast-resolve-type.h
index 3a7dbd6..5870aca 100644
--- a/gcc/rust/resolve/rust-ast-resolve-type.h
+++ b/gcc/rust/resolve/rust-ast-resolve-type.h
@@ -61,37 +61,6 @@ class ResolveType : public ResolverBase
using Rust::Resolver::ResolverBase::visit;
public:
- static NodeId go (AST::TypePath &type_path)
- {
- return ResolveType::go ((AST::Type &) type_path);
- }
-
- static NodeId go (AST::Path &type_path)
- {
- if (type_path.get_path_kind () == AST::Path::Kind::LangItem)
- {
- auto &type = static_cast<AST::LangItemPath &> (type_path);
-
- auto lang_item = Analysis::Mappings::get ()
- .lookup_lang_item_node (type.get_lang_item_kind ())
- .value ();
-
- auto resolver = Resolver::get ();
- resolver->insert_resolved_type (type.get_node_id (), lang_item);
-
- return lang_item;
- }
-
- rust_assert (type_path.get_path_kind () == AST::Path::Kind::Type);
-
- // We have to do this dance to first downcast to a typepath, and then upcast
- // to a Type. The altnernative is to split `go` into `go` and `go_inner` or
- // something, but eventually this will need to change as we'll need
- // `ResolveType::` to resolve other kinds of `Path`s as well.
- return ResolveType::go (
- (AST::Type &) static_cast<AST::TypePath &> (type_path));
- }
-
static NodeId go (AST::Type &type)
{
ResolveType resolver;
diff --git a/gcc/rust/resolve/rust-late-name-resolver-2.0.cc b/gcc/rust/resolve/rust-late-name-resolver-2.0.cc
index 974e1fa..60b8952 100644
--- a/gcc/rust/resolve/rust-late-name-resolver-2.0.cc
+++ b/gcc/rust/resolve/rust-late-name-resolver-2.0.cc
@@ -249,25 +249,6 @@ Late::visit (AST::PathInExpression &expr)
}
void
-Late::visit (AST::LangItemPath &type)
-{
- auto &mappings = Rust::Analysis::Mappings::get ();
- auto lang_item = mappings.lookup_lang_item_node (type.get_lang_item_kind ());
-
- if (!lang_item)
- {
- rust_fatal_error (
- type.get_locus (), "use of undeclared lang item %qs",
- LangItem::ToString (type.get_lang_item_kind ()).c_str ());
- return;
- }
-
- ctx.map_usage (Usage (type.get_node_id ()), Definition (lang_item.value ()));
-
- DefaultResolver::visit (type);
-}
-
-void
Late::visit (AST::TypePath &type)
{
// should we add type path resolution in `ForeverStack` directly? Since it's
diff --git a/gcc/rust/resolve/rust-late-name-resolver-2.0.h b/gcc/rust/resolve/rust-late-name-resolver-2.0.h
index 0db21f2..0efa693 100644
--- a/gcc/rust/resolve/rust-late-name-resolver-2.0.h
+++ b/gcc/rust/resolve/rust-late-name-resolver-2.0.h
@@ -46,7 +46,6 @@ public:
// resolutions
void visit (AST::IdentifierExpr &) override;
void visit (AST::PathInExpression &) override;
- void visit (AST::LangItemPath &) override;
void visit (AST::TypePath &) override;
void visit (AST::Trait &) override;
void visit (AST::StructExprStruct &) override;