aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilip Herron <philip.herron@embecosm.com>2022-10-14 14:00:16 +0100
committerArthur Cohen <arthur.cohen@embecosm.com>2023-02-21 12:36:34 +0100
commit74065c000e33481c209affbcc00dc58472bf2170 (patch)
tree70622fe4702dc6c00acb7605642ae40ce2490acb
parent173f98ae49d816178d8fa5b627c62aade9547d04 (diff)
downloadgcc-74065c000e33481c209affbcc00dc58472bf2170.zip
gcc-74065c000e33481c209affbcc00dc58472bf2170.tar.gz
gcc-74065c000e33481c209affbcc00dc58472bf2170.tar.bz2
gccrs: Refactor TraitResolver to not require a visitor
gcc/rust/ChangeLog: * typecheck/rust-hir-trait-resolve.cc (TraitResolver::TraitResolver): Do not nullptr init `resolved_trait_reference` anymore. (TraitResolver::resolve_path): Simplify function and rename to... (TraitResolver::resolve_path_to_trait): ...this. (TraitResolver::lookup_path): Use new interface. * typecheck/rust-hir-trait-resolve.h (class TraitResolver): Do not inherit `HIRFullVisitor` class anymore.
-rw-r--r--gcc/rust/typecheck/rust-hir-trait-resolve.cc53
-rw-r--r--gcc/rust/typecheck/rust-hir-trait-resolve.h10
2 files changed, 26 insertions, 37 deletions
diff --git a/gcc/rust/typecheck/rust-hir-trait-resolve.cc b/gcc/rust/typecheck/rust-hir-trait-resolve.cc
index 22398b1..1b0bcaa 100644
--- a/gcc/rust/typecheck/rust-hir-trait-resolve.cc
+++ b/gcc/rust/typecheck/rust-hir-trait-resolve.cc
@@ -91,33 +91,42 @@ TraitResolver::Lookup (HIR::TypePath &path)
return resolver.lookup_path (path);
}
-TraitResolver::TraitResolver ()
- : TypeCheckBase (), resolved_trait_reference (nullptr)
-{}
+TraitResolver::TraitResolver () : TypeCheckBase () {}
-TraitReference *
-TraitResolver::resolve_path (HIR::TypePath &path)
+bool
+TraitResolver::resolve_path_to_trait (const HIR::TypePath &path,
+ HIR::Trait **resolved) const
{
NodeId ref;
if (!resolver->lookup_resolved_type (path.get_mappings ().get_nodeid (),
&ref))
{
rust_error_at (path.get_locus (), "Failed to resolve path to node-id");
- return &TraitReference::error_node ();
+ return false;
}
HirId hir_node = UNKNOWN_HIRID;
if (!mappings->lookup_node_to_hir (ref, &hir_node))
{
rust_error_at (path.get_locus (), "Failed to resolve path to hir-id");
- return &TraitReference::error_node ();
+ return false;
}
HIR::Item *resolved_item = mappings->lookup_hir_item (hir_node);
-
rust_assert (resolved_item != nullptr);
- resolved_item->accept_vis (*this);
- rust_assert (resolved_trait_reference != nullptr);
+ rust_assert (resolved_item->get_item_kind () == HIR::Item::ItemKind::Trait);
+ *resolved = static_cast<HIR::Trait *> (resolved_item);
+
+ return true;
+}
+
+TraitReference *
+TraitResolver::resolve_path (HIR::TypePath &path)
+{
+ HIR::Trait *resolved_trait_reference;
+ bool ok = resolve_path_to_trait (path, &resolved_trait_reference);
+ if (!ok)
+ return &TraitReference::error_node ();
return resolve_trait (resolved_trait_reference);
}
@@ -237,26 +246,10 @@ TraitResolver::resolve_trait (HIR::Trait *trait_reference)
TraitReference *
TraitResolver::lookup_path (HIR::TypePath &path)
{
- NodeId ref;
- if (!resolver->lookup_resolved_type (path.get_mappings ().get_nodeid (),
- &ref))
- {
- rust_error_at (path.get_locus (), "Failed to resolve path to node-id");
- return &TraitReference::error_node ();
- }
-
- HirId hir_node = UNKNOWN_HIRID;
- if (!mappings->lookup_node_to_hir (ref, &hir_node))
- {
- rust_error_at (path.get_locus (), "Failed to resolve path to hir-id");
- return &TraitReference::error_node ();
- }
-
- HIR::Item *resolved_item = mappings->lookup_hir_item (hir_node);
-
- rust_assert (resolved_item != nullptr);
- resolved_item->accept_vis (*this);
- rust_assert (resolved_trait_reference != nullptr);
+ HIR::Trait *resolved_trait_reference;
+ bool ok = resolve_path_to_trait (path, &resolved_trait_reference);
+ if (!ok)
+ return &TraitReference::error_node ();
TraitReference *tref = &TraitReference::error_node ();
if (context->lookup_trait_reference (
diff --git a/gcc/rust/typecheck/rust-hir-trait-resolve.h b/gcc/rust/typecheck/rust-hir-trait-resolve.h
index 6d4b932..ca23d48 100644
--- a/gcc/rust/typecheck/rust-hir-trait-resolve.h
+++ b/gcc/rust/typecheck/rust-hir-trait-resolve.h
@@ -55,10 +55,8 @@ private:
std::vector<TyTy::SubstitutionParamMapping> substitutions;
};
-class TraitResolver : public TypeCheckBase, private HIR::HIRFullVisitorBase
+class TraitResolver : public TypeCheckBase
{
- using HIR::HIRFullVisitorBase::visit;
-
public:
static TraitReference *Resolve (HIR::TypePath &path);
@@ -75,10 +73,8 @@ private:
TraitReference *lookup_path (HIR::TypePath &path);
- HIR::Trait *resolved_trait_reference;
-
-public:
- void visit (HIR::Trait &trait) override { resolved_trait_reference = &trait; }
+ bool resolve_path_to_trait (const HIR::TypePath &path,
+ HIR::Trait **resolved) const;
};
} // namespace Resolver