Line data Source code
1 : // Copyright (C) 2020-2026 Free Software Foundation, Inc.
2 :
3 : // This file is part of GCC.
4 :
5 : // GCC is free software; you can redistribute it and/or modify it under
6 : // the terms of the GNU General Public License as published by the Free
7 : // Software Foundation; either version 3, or (at your option) any later
8 : // version.
9 :
10 : // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 : // WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 : // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 : // for more details.
14 :
15 : // You should have received a copy of the GNU General Public License
16 : // along with GCC; see the file COPYING3. If not see
17 : // <http://www.gnu.org/licenses/>.
18 :
19 : #include "rust-reachability.h"
20 : #include "rust-tyty.h"
21 :
22 : namespace Rust {
23 : namespace Privacy {
24 :
25 : static HIR::VisItem *
26 5790 : maybe_get_vis_item (std::unique_ptr<HIR::Item> &item)
27 : {
28 5790 : if (item->get_hir_kind () != HIR::Node::VIS_ITEM)
29 : return nullptr;
30 :
31 5790 : return static_cast<HIR::VisItem *> (item.get ());
32 : }
33 :
34 : ReachLevel
35 23453 : ReachabilityVisitor::get_reachability_level (
36 : const HIR::Visibility &item_visibility)
37 : {
38 23453 : return item_visibility.is_public () ? current_level : ReachLevel::Unreachable;
39 : }
40 :
41 : void
42 21574 : ReachabilityVisitor::visit_generic_predicates (
43 : const std::vector<std::unique_ptr<HIR::GenericParam>> &generics,
44 : ReachLevel item_reach)
45 : {
46 21574 : if (item_reach == ReachLevel::Unreachable)
47 : return;
48 :
49 9701 : for (const auto &generic : generics)
50 : {
51 4598 : if (generic->get_kind () == HIR::GenericParam::GenericKind::TYPE)
52 : {
53 4560 : TyTy::BaseType *ty = nullptr;
54 4560 : auto ok
55 4560 : = ty_ctx.lookup_type (generic->get_mappings ().get_hirid (), &ty);
56 4560 : rust_assert (ok);
57 :
58 9627 : for (const auto &bound : ty->get_specified_bounds ())
59 : {
60 5067 : const auto trait = bound.get ()->get_hir_trait_ref ();
61 5067 : ctx.update_reachability (trait->get_mappings (), item_reach);
62 : }
63 : }
64 : }
65 : }
66 :
67 : void
68 1374 : ReachabilityVisitor::visit (HIR::Module &mod)
69 : {
70 1374 : auto reach = get_reachability_level (mod.get_visibility ());
71 1374 : reach = ctx.update_reachability (mod.get_mappings (), reach);
72 :
73 7164 : for (auto &item : mod.get_items ())
74 : {
75 : // FIXME: Is that what we want to do? Yes? Only visit the items with
76 : // visibility?
77 : //
78 : // Imagine if we had `maybe_get_vis_item(item)?->accept_vis(*this)` ;)
79 5790 : auto vis_item = maybe_get_vis_item (item);
80 5790 : if (vis_item)
81 5790 : vis_item->accept_vis (*this);
82 : }
83 1374 : }
84 :
85 : void
86 0 : ReachabilityVisitor::visit (HIR::ExternCrate &crate)
87 : {
88 0 : auto reach = get_reachability_level (crate.get_visibility ());
89 0 : reach = ctx.update_reachability (crate.get_mappings (), reach);
90 0 : }
91 :
92 : void
93 0 : ReachabilityVisitor::visit (HIR::UseDeclaration &use_decl)
94 : {
95 0 : auto reach = get_reachability_level (use_decl.get_visibility ());
96 0 : reach = ctx.update_reachability (use_decl.get_mappings (), reach);
97 0 : }
98 :
99 : void
100 6998 : ReachabilityVisitor::visit (HIR::Function &func)
101 : {
102 6998 : auto fn_reach = get_reachability_level (func.get_visibility ());
103 :
104 6998 : fn_reach = ctx.update_reachability (func.get_mappings (), fn_reach);
105 6998 : visit_generic_predicates (func.get_generic_params (), fn_reach);
106 6998 : }
107 :
108 : void
109 61 : ReachabilityVisitor::visit (HIR::TypeAlias &type_alias)
110 : {
111 61 : auto type_reach = get_reachability_level (type_alias.get_visibility ());
112 :
113 61 : visit_generic_predicates (type_alias.get_generic_params (), type_reach);
114 61 : }
115 :
116 : void
117 1543 : ReachabilityVisitor::visit (HIR::StructStruct &struct_item)
118 : {
119 1543 : auto struct_reach = get_reachability_level (struct_item.get_visibility ());
120 :
121 1543 : struct_reach
122 1543 : = ctx.update_reachability (struct_item.get_mappings (), struct_reach);
123 :
124 1543 : auto old_level = current_level;
125 1543 : current_level = struct_reach;
126 :
127 1543 : visit_generic_predicates (struct_item.get_generic_params (), struct_reach);
128 :
129 1543 : if (struct_reach != ReachLevel::Unreachable)
130 : {
131 762 : for (auto &field : struct_item.get_fields ())
132 368 : if (field.get_visibility ().is_public ())
133 267 : ctx.update_reachability (field.get_field_type ().get_mappings (),
134 : struct_reach);
135 : }
136 :
137 1543 : current_level = old_level;
138 1543 : }
139 :
140 : void
141 858 : ReachabilityVisitor::visit (HIR::TupleStruct &)
142 858 : {}
143 :
144 : void
145 562 : ReachabilityVisitor::visit (HIR::Enum &enum_item)
146 : {
147 562 : auto enum_reach = get_reachability_level (enum_item.get_visibility ());
148 :
149 562 : enum_reach = ctx.update_reachability (enum_item.get_mappings (), enum_reach);
150 562 : visit_generic_predicates (enum_item.get_generic_params (), enum_reach);
151 :
152 1822 : for (const auto &variant : enum_item.get_variants ())
153 : {
154 1260 : auto variant_reach
155 1260 : = ctx.update_reachability (variant->get_mappings (), enum_reach);
156 :
157 1260 : switch (variant->get_enum_item_kind ())
158 : {
159 449 : case HIR::EnumItem::Tuple:
160 449 : {
161 : // Should we update the fields only if they are public? Similarly to
162 : // what we do in the ReachabilityVisitor for HIR::TupleStruct?
163 449 : auto tuple_variant
164 449 : = static_cast<HIR::EnumItemTuple *> (variant.get ());
165 961 : for (const auto &field : tuple_variant->get_tuple_fields ())
166 512 : ctx.update_reachability (field.get_mappings (), variant_reach);
167 : break;
168 : }
169 80 : case HIR::EnumItem::Struct:
170 80 : {
171 : // Should we update the fields only if they are public? Similarly to
172 : // what we do in the ReachabilityVisitor for HIR::StructStruct?
173 80 : auto struct_variant
174 80 : = static_cast<HIR::EnumItemStruct *> (variant.get ());
175 211 : for (const auto &field : struct_variant->get_struct_fields ())
176 131 : ctx.update_reachability (field.get_mappings (), variant_reach);
177 : break;
178 : }
179 : // Nothing nested to visit in that case
180 : case HIR::EnumItem::Named:
181 : case HIR::EnumItem::Discriminant:
182 : break;
183 : }
184 : }
185 562 : }
186 :
187 : void
188 102 : ReachabilityVisitor::visit (HIR::Union &union_item)
189 : {
190 102 : auto union_reach = get_reachability_level (union_item.get_visibility ());
191 :
192 102 : union_reach
193 102 : = ctx.update_reachability (union_item.get_mappings (), union_reach);
194 102 : visit_generic_predicates (union_item.get_generic_params (), union_reach);
195 102 : }
196 :
197 : void
198 431 : ReachabilityVisitor::visit (HIR::ConstantItem &const_item)
199 : {
200 431 : auto reach = get_reachability_level (const_item.get_visibility ());
201 431 : reach = ctx.update_reachability (const_item.get_mappings (), reach);
202 431 : }
203 :
204 : void
205 74 : ReachabilityVisitor::visit (HIR::StaticItem &static_item)
206 : {
207 74 : auto reach = get_reachability_level (static_item.get_visibility ());
208 74 : reach = ctx.update_reachability (static_item.get_mappings (), reach);
209 74 : }
210 :
211 : void
212 4328 : ReachabilityVisitor::visit (HIR::Trait &trait)
213 : {
214 4328 : auto trait_reach = get_reachability_level (trait.get_visibility ());
215 :
216 4328 : trait_reach = ctx.update_reachability (trait.get_mappings (), trait_reach);
217 4328 : visit_generic_predicates (trait.get_generic_params (), trait_reach);
218 4328 : }
219 :
220 : void
221 7980 : ReachabilityVisitor::visit (HIR::ImplBlock &impl)
222 : {
223 7980 : auto impl_reach = get_reachability_level (impl.get_visibility ());
224 :
225 7980 : impl_reach = ctx.update_reachability (impl.get_mappings (), impl_reach);
226 7980 : visit_generic_predicates (impl.get_generic_params (), impl_reach);
227 7980 : }
228 :
229 : void
230 1830 : ReachabilityVisitor::visit (HIR::ExternBlock &)
231 1830 : {}
232 :
233 : // FIXME: How can we visit Blocks in the current configuration? Have a full
234 : // visitor?
235 : } // namespace Privacy
236 : } // namespace Rust
|