aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/checks/errors/privacy/rust-reachability.h
blob: 7395a0f77dbc4ac92878b4eb82c53fe0a26f7758 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright (C) 2020-2025 Free Software Foundation, Inc.

// This file is part of GCC.

// GCC is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 3, or (at your option) any later
// version.

// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
// for more details.

// You should have received a copy of the GNU General Public License
// along with GCC; see the file COPYING3.  If not see
// <http://www.gnu.org/licenses/>.

#ifndef RUST_REACHABILITY_H
#define RUST_REACHABILITY_H

#include "rust-privacy-ctx.h"
#include "rust-hir-visitor.h"
#include "rust-hir.h"
#include "rust-hir-expr.h"
#include "rust-hir-stmt.h"
#include "rust-hir-item.h"
#include "rust-hir-type-check.h"

namespace Rust {
namespace Privacy {

// FIXME: The EmbargoVisitor from rustc is a fixed-point visitor which tries
// to reach more and more nodes until nothing has changed anymore.
// Do we need to reproduce this behavior? How long does it take to do this?

/**
 * The ReachabilityVisitor tries to reach all items possible in the crate,
 * according to their privacy level.
 */
class ReachabilityVisitor : public HIR::HIRVisItemVisitor
{
public:
  ReachabilityVisitor (PrivacyContext &ctx,
		       const ::Rust::Resolver::TypeCheckContext &ty_ctx)
    : current_level (ReachLevel::Reachable), ctx (ctx), ty_ctx (ty_ctx)
  {}

  // FIXME: Add `go` method which takes an `HIR::Crate &` as argument

  /**
   * 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);

  /**
   * Get the initial reach level for an item based on its visibility.
   */
  ReachLevel get_reachability_level (const HIR::Visibility &item_visibility);

  virtual void visit (HIR::Module &mod);
  virtual void visit (HIR::ExternCrate &crate);
  virtual void visit (HIR::UseDeclaration &use_decl);
  virtual void visit (HIR::Function &func);
  virtual void visit (HIR::TypeAlias &type_alias);
  virtual void visit (HIR::StructStruct &struct_item);
  virtual void visit (HIR::TupleStruct &tuple_struct);
  virtual void visit (HIR::Enum &enum_item);
  virtual void visit (HIR::Union &union_item);
  virtual void visit (HIR::ConstantItem &const_item);
  virtual void visit (HIR::StaticItem &static_item);
  virtual void visit (HIR::Trait &trait);
  virtual void visit (HIR::ImplBlock &impl);
  virtual void visit (HIR::ExternBlock &block);

private:
  ReachLevel current_level;
  PrivacyContext &ctx;
  const ::Rust::Resolver::TypeCheckContext &ty_ctx;
};
} // namespace Privacy
} // namespace Rust

#endif // !RUST_REACHABILITY_H