aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/checks/errors/privacy/rust-visibility-resolver.h
blob: 4dfba4cb86b4bdbeb962d00003c274a4046e7517 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// 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_VISIBILITY_H
#define RUST_VISIBILITY_H

#include "rust-hir.h"
#include "rust-hir-expr.h"
#include "rust-hir-stmt.h"
#include "rust-hir-item.h"
#include "rust-hir-map.h"
#include "rust-name-resolver.h"
#include "rust-hir-visitor.h"

namespace Rust {
namespace Privacy {

class VisibilityResolver : public HIR::HIRVisItemVisitor
{
public:
  VisibilityResolver (Analysis::Mappings &mappings,
		      Rust::Resolver::Resolver &resolver);

  /**
   * Perform visibility resolving on an entire crate
   */
  void go (HIR::Crate &crate);

  /**
   * Resolve a path to the module it refers
   */
  bool resolve_module_path (const HIR::SimplePath &restriction,
			    DefId &to_resolve);

  /**
   * Resolve the visibility of an item to its ModuleVisibility. This function
   * emits errors if necessary. The contents of the to_resolve parameter will be
   * overwritten on success.
   *
   * @param visibility Visibility of the item to resolve
   * @param to_resolve ModuleVisibility reference to fill on success.
   *
   * @return false on error, true if the resolving was successful.
   */
  bool resolve_visibility (const HIR::Visibility &visibility,
			   ModuleVisibility &to_resolve);

  /**
   * Resolve the visibility of an item and updates it. This is useful for
   * vis-items who need to be resolved but do not care about their module
   * visibility - const items, static items, etc. For items with an impact on
   * their children (enums, traits), this cannot be used
   */
  void resolve_and_update (const HIR::VisItem *item);

  /**
   * Get the DefId of the parent module we are currently visiting.
   *
   * @return UNKNOWN_DEFID if the module stack is empty, a valid `DefId`
   * otherwise
   */
  DefId peek_module ();

  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:
  Analysis::Mappings &mappings;
  Rust::Resolver::Resolver &resolver;
  DefId current_module;
};

} // namespace Privacy
} // namespace Rust

#endif // !RUST_VISIBILITY_H