aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust
diff options
context:
space:
mode:
authorArthur Cohen <arthur.cohen@embecosm.com>2022-04-12 16:33:29 +0200
committerArthur Cohen <arthur.cohen@embecosm.com>2022-04-13 13:15:10 +0200
commitcdfb5b34ac243c9c3c7c5b24f9a7409e0ebd459f (patch)
treebd2f711bf110d3f55af3237d42eae49d983978c0 /gcc/rust
parenta196568774872fb1312b500068dc122c26536b34 (diff)
downloadgcc-cdfb5b34ac243c9c3c7c5b24f9a7409e0ebd459f.zip
gcc-cdfb5b34ac243c9c3c7c5b24f9a7409e0ebd459f.tar.gz
gcc-cdfb5b34ac243c9c3c7c5b24f9a7409e0ebd459f.tar.bz2
privacy: reachability: Cleanup Struct definition visitor
Diffstat (limited to 'gcc/rust')
-rw-r--r--gcc/rust/privacy/rust-reachability.cc67
-rw-r--r--gcc/rust/privacy/rust-reachability.h8
2 files changed, 42 insertions, 33 deletions
diff --git a/gcc/rust/privacy/rust-reachability.cc b/gcc/rust/privacy/rust-reachability.cc
index 82a4827..ea7ca24 100644
--- a/gcc/rust/privacy/rust-reachability.cc
+++ b/gcc/rust/privacy/rust-reachability.cc
@@ -32,6 +32,38 @@ maybe_get_vis_item (std::unique_ptr<HIR::Item> &item)
}
void
+ReachabilityVisitor::visit_generic_predicates (
+ const std::vector<std::unique_ptr<HIR::GenericParam>> &generics,
+ ReachLevel item_reach)
+{
+ if (item_reach == ReachLevel::Unreachable)
+ return;
+
+ for (auto &generic : generics)
+ {
+ if (generic->get_kind () == HIR::GenericParam::TYPE)
+ {
+ TyTy::BaseType *generic_ty = nullptr;
+ rust_assert (
+ ty_ctx.lookup_type (generic->get_mappings ().get_hirid (),
+ &generic_ty));
+
+ // FIXME: Can we really get anything else than a TyTy::PARAM here?
+ // Should we change this to an assertion instead?
+ if (generic_ty->get_kind () == TyTy::PARAM)
+ {
+ auto generic_param = static_cast<TyTy::ParamType *> (generic_ty);
+ for (const auto &bound : generic_param->get_specified_bounds ())
+ {
+ const auto trait = bound.get ()->get_hir_trait_ref ();
+ ctx.update_reachability (trait->get_mappings (), item_reach);
+ }
+ }
+ }
+ }
+}
+
+void
ReachabilityVisitor::visit (HIR::Module &mod)
{
for (auto &item : mod.get_items ())
@@ -75,43 +107,12 @@ ReachabilityVisitor::visit (HIR::StructStruct &struct_item)
auto old_level = current_level;
current_level = struct_reach;
+ visit_generic_predicates (struct_item.get_generic_params (), struct_reach);
+
if (struct_reach != ReachLevel::Unreachable)
{
for (auto &field : struct_item.get_fields ())
if (field.get_visibility ().is_public ())
- ctx.update_reachability (field.get_mappings (), struct_reach);
-
- for (auto &generic : struct_item.get_generic_params ())
- {
- switch (generic->get_kind ())
- {
- case HIR::GenericParam::LIFETIME:
- break;
- case HIR::GenericParam::TYPE:
- TyTy::BaseType *generic_ty = nullptr;
- rust_assert (
- ty_ctx.lookup_type (generic->get_mappings ().get_hirid (),
- &generic_ty));
-
- if (generic_ty->get_kind () == TyTy::PARAM)
- {
- auto generic_param
- = static_cast<TyTy::ParamType *> (generic_ty);
- for (const auto &bound :
- generic_param->get_specified_bounds ())
- {
- const auto trait = bound.get ()->get_hir_trait_ref ();
- ctx.update_reachability (trait->get_mappings (),
- struct_reach);
- }
- }
-
- break;
- }
- }
-
- for (auto &field : struct_item.get_fields ())
- if (field.get_visibility ().is_public ())
ctx.update_reachability (field.get_field_type ()->get_mappings (),
struct_reach);
}
diff --git a/gcc/rust/privacy/rust-reachability.h b/gcc/rust/privacy/rust-reachability.h
index 75ed269..8d740c8 100644
--- a/gcc/rust/privacy/rust-reachability.h
+++ b/gcc/rust/privacy/rust-reachability.h
@@ -46,6 +46,14 @@ public:
: current_level (ReachLevel::Reachable), ctx (ctx), ty_ctx (ty_ctx)
{}
+ /**
+ * Visit all the predicates of all the generic types of a given item, marking
+ * them as reachable or not.
+ */
+ void visit_generic_predicates (
+ const std::vector<std::unique_ptr<HIR::GenericParam>> &generics,
+ ReachLevel item_reach);
+
virtual void visit (HIR::Module &mod);
virtual void visit (HIR::ExternCrate &crate);
virtual void visit (HIR::UseDeclaration &use_decl);