aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/checks/errors/privacy/rust-privacy-ctx.h
blob: a506613f64e8b3f554e39b661033aa5ab2d46e56 (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
// 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_PRIVACY_CTX_H
#define RUST_PRIVACY_CTX_H

#include "rust-hir-map.h"
#include "rust-privacy-check.h"

namespace Rust {
namespace Privacy {

/**
 * Reachability levels of HIR nodes. These levels are computed through the
 * `ReachabilityVisitor` visitor.
 */
enum ReachLevel
{
  Unreachable,
  Reachable,
};

class PrivacyContext
{
public:
  /**
   * Insert a new resolved visibility for a given node. If the node is already
   * present in the reachability map, then its visibility will only be updated
   * if the given visibility is higher.
   *
   * @param mappings Mappings of the node to store the reach level for
   * @param reach Level of reachability for the given node
   *
   * @return The new reachability level for this node. If this was the first
   * time inserting this node, then return `reach`. Otherwise, return `reach` or
   * the existing reach level if it was higher.
   */
  ReachLevel update_reachability (const Analysis::NodeMapping &mapping,
				  ReachLevel reach);

  /**
   * Lookup the visibility of an already declared Node
   *
   * @param mapping Mappings of the node to fetch the reach level of
   *
   * @return `nullptr` if the reach level for the current node has not been
   * added, a valid pointer otherwise
   */
  const ReachLevel *lookup_reachability (const Analysis::NodeMapping &mapping);

private:
  std::unordered_map<DefId, ReachLevel> reachability_map;
};
} // namespace Privacy
} // namespace Rust

#if CHECKING_P
namespace selftest {
void
rust_privacy_ctx_test (void);
}
#endif // !CHECKING_P

#endif // !RUST_PRIVACY_CTX_H