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 : #ifndef RUST_REACHABILITY_H
20 : #define RUST_REACHABILITY_H
21 :
22 : #include "rust-privacy-ctx.h"
23 : #include "rust-hir-visitor.h"
24 : #include "rust-hir.h"
25 : #include "rust-hir-expr.h"
26 : #include "rust-hir-stmt.h"
27 : #include "rust-hir-item.h"
28 : #include "rust-hir-type-check.h"
29 :
30 : namespace Rust {
31 : namespace Privacy {
32 :
33 : // FIXME: The EmbargoVisitor from rustc is a fixed-point visitor which tries
34 : // to reach more and more nodes until nothing has changed anymore.
35 : // Do we need to reproduce this behavior? How long does it take to do this?
36 :
37 : /**
38 : * The ReachabilityVisitor tries to reach all items possible in the crate,
39 : * according to their privacy level.
40 : */
41 : class ReachabilityVisitor : public HIR::HIRVisItemVisitor
42 : {
43 : public:
44 4121 : ReachabilityVisitor (PrivacyContext &ctx,
45 : const ::Rust::Resolver::TypeCheckContext &ty_ctx)
46 4121 : : current_level (ReachLevel::Reachable), ctx (ctx), ty_ctx (ty_ctx)
47 : {}
48 :
49 : // FIXME: Add `go` method which takes an `HIR::Crate &` as argument
50 :
51 : /**
52 : * Visit all the predicates of all the generic types of a given item, marking
53 : * them as reachable or not.
54 : */
55 : void visit_generic_predicates (
56 : const std::vector<std::unique_ptr<HIR::GenericParam>> &generics,
57 : ReachLevel item_reach);
58 :
59 : /**
60 : * Get the initial reach level for an item based on its visibility.
61 : */
62 : ReachLevel get_reachability_level (const HIR::Visibility &item_visibility);
63 :
64 : virtual void visit (HIR::Module &mod);
65 : virtual void visit (HIR::ExternCrate &crate);
66 : virtual void visit (HIR::UseDeclaration &use_decl);
67 : virtual void visit (HIR::Function &func);
68 : virtual void visit (HIR::TypeAlias &type_alias);
69 : virtual void visit (HIR::StructStruct &struct_item);
70 : virtual void visit (HIR::TupleStruct &tuple_struct);
71 : virtual void visit (HIR::Enum &enum_item);
72 : virtual void visit (HIR::Union &union_item);
73 : virtual void visit (HIR::ConstantItem &const_item);
74 : virtual void visit (HIR::StaticItem &static_item);
75 : virtual void visit (HIR::Trait &trait);
76 : virtual void visit (HIR::ImplBlock &impl);
77 : virtual void visit (HIR::ExternBlock &block);
78 :
79 : private:
80 : ReachLevel current_level;
81 : PrivacyContext &ctx;
82 : const ::Rust::Resolver::TypeCheckContext &ty_ctx;
83 : };
84 : } // namespace Privacy
85 : } // namespace Rust
86 :
87 : #endif // !RUST_REACHABILITY_H
|