Skip to content

Commit ed904fe

Browse files
privacy: reachability: Add base visitor for items with generic params
Co-authored-by: philberty <philip.herron@embecosm.com>
1 parent cdfb5b3 commit ed904fe

File tree

2 files changed

+58
-22
lines changed

2 files changed

+58
-22
lines changed

gcc/rust/privacy/rust-reachability.cc

Lines changed: 53 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ maybe_get_vis_item (std::unique_ptr<HIR::Item> &item)
3131
return static_cast<HIR::VisItem *> (item.get ());
3232
}
3333

34+
ReachLevel
35+
ReachabilityVisitor::get_reachability_level (
36+
const HIR::Visibility &item_visibility)
37+
{
38+
return item_visibility.is_public () ? current_level : ReachLevel::Unreachable;
39+
}
40+
3441
void
3542
ReachabilityVisitor::visit_generic_predicates (
3643
const std::vector<std::unique_ptr<HIR::GenericParam>> &generics,
@@ -39,25 +46,21 @@ ReachabilityVisitor::visit_generic_predicates (
3946
if (item_reach == ReachLevel::Unreachable)
4047
return;
4148

42-
for (auto &generic : generics)
49+
for (const auto &generic : generics)
4350
{
4451
if (generic->get_kind () == HIR::GenericParam::TYPE)
4552
{
4653
TyTy::BaseType *generic_ty = nullptr;
47-
rust_assert (
48-
ty_ctx.lookup_type (generic->get_mappings ().get_hirid (),
49-
&generic_ty));
54+
auto ok = ty_ctx.lookup_type (generic->get_mappings ().get_hirid (),
55+
&generic_ty);
56+
rust_assert (ok);
57+
rust_assert (generic_ty->get_kind () == TyTy::PARAM);
5058

51-
// FIXME: Can we really get anything else than a TyTy::PARAM here?
52-
// Should we change this to an assertion instead?
53-
if (generic_ty->get_kind () == TyTy::PARAM)
59+
auto generic_param = static_cast<TyTy::ParamType *> (generic_ty);
60+
for (const auto &bound : generic_param->get_specified_bounds ())
5461
{
55-
auto generic_param = static_cast<TyTy::ParamType *> (generic_ty);
56-
for (const auto &bound : generic_param->get_specified_bounds ())
57-
{
58-
const auto trait = bound.get ()->get_hir_trait_ref ();
59-
ctx.update_reachability (trait->get_mappings (), item_reach);
60-
}
62+
const auto trait = bound.get ()->get_hir_trait_ref ();
63+
ctx.update_reachability (trait->get_mappings (), item_reach);
6164
}
6265
}
6366
}
@@ -88,18 +91,25 @@ ReachabilityVisitor::visit (HIR::UseDeclaration &use_decl)
8891

8992
void
9093
ReachabilityVisitor::visit (HIR::Function &func)
91-
{}
94+
{
95+
auto fn_reach = get_reachability_level (func.get_visibility ());
96+
97+
fn_reach = ctx.update_reachability (func.get_mappings (), fn_reach);
98+
visit_generic_predicates (func.get_generic_params (), fn_reach);
99+
}
92100

93101
void
94102
ReachabilityVisitor::visit (HIR::TypeAlias &type_alias)
95-
{}
103+
{
104+
auto type_reach = get_reachability_level (type_alias.get_visibility ());
105+
106+
visit_generic_predicates (type_alias.get_generic_params (), type_reach);
107+
}
96108

97109
void
98110
ReachabilityVisitor::visit (HIR::StructStruct &struct_item)
99111
{
100-
auto struct_reach = ReachLevel::Unreachable;
101-
if (struct_item.get_visibility ().is_public ())
102-
struct_reach = current_level;
112+
auto struct_reach = get_reachability_level (struct_item.get_visibility ());
103113

104114
struct_reach
105115
= ctx.update_reachability (struct_item.get_mappings (), struct_reach);
@@ -126,11 +136,22 @@ ReachabilityVisitor::visit (HIR::TupleStruct &tuple_struct)
126136

127137
void
128138
ReachabilityVisitor::visit (HIR::Enum &enum_item)
129-
{}
139+
{
140+
auto enum_reach = get_reachability_level (enum_item.get_visibility ());
141+
142+
enum_reach = ctx.update_reachability (enum_item.get_mappings (), enum_reach);
143+
visit_generic_predicates (enum_item.get_generic_params (), enum_reach);
144+
}
130145

131146
void
132147
ReachabilityVisitor::visit (HIR::Union &union_item)
133-
{}
148+
{
149+
auto union_reach = get_reachability_level (union_item.get_visibility ());
150+
151+
union_reach
152+
= ctx.update_reachability (union_item.get_mappings (), union_reach);
153+
visit_generic_predicates (union_item.get_generic_params (), union_reach);
154+
}
134155

135156
void
136157
ReachabilityVisitor::visit (HIR::ConstantItem &const_item)
@@ -142,11 +163,21 @@ ReachabilityVisitor::visit (HIR::StaticItem &static_item)
142163

143164
void
144165
ReachabilityVisitor::visit (HIR::Trait &trait)
145-
{}
166+
{
167+
auto trait_reach = get_reachability_level (trait.get_visibility ());
168+
169+
trait_reach = ctx.update_reachability (trait.get_mappings (), trait_reach);
170+
visit_generic_predicates (trait.get_generic_params (), trait_reach);
171+
}
146172

147173
void
148174
ReachabilityVisitor::visit (HIR::ImplBlock &impl)
149-
{}
175+
{
176+
auto impl_reach = get_reachability_level (impl.get_visibility ());
177+
178+
impl_reach = ctx.update_reachability (impl.get_mappings (), impl_reach);
179+
visit_generic_predicates (impl.get_generic_params (), impl_reach);
180+
}
150181

151182
void
152183
ReachabilityVisitor::visit (HIR::ExternBlock &block)

gcc/rust/privacy/rust-reachability.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ class ReachabilityVisitor : public HIR::HIRVisItemVisitor
5454
const std::vector<std::unique_ptr<HIR::GenericParam>> &generics,
5555
ReachLevel item_reach);
5656

57+
/**
58+
* Get the initial reach level for an item based on its visibility.
59+
*/
60+
ReachLevel get_reachability_level (const HIR::Visibility &item_visibility);
61+
5762
virtual void visit (HIR::Module &mod);
5863
virtual void visit (HIR::ExternCrate &crate);
5964
virtual void visit (HIR::UseDeclaration &use_decl);

0 commit comments

Comments
 (0)