diff options
Diffstat (limited to 'crates/ra_hir_def/src/visibility.rs')
-rw-r--r-- | crates/ra_hir_def/src/visibility.rs | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/crates/ra_hir_def/src/visibility.rs b/crates/ra_hir_def/src/visibility.rs new file mode 100644 index 000000000..d8296da4b --- /dev/null +++ b/crates/ra_hir_def/src/visibility.rs | |||
@@ -0,0 +1,120 @@ | |||
1 | //! Defines hir-level representation of visibility (e.g. `pub` and `pub(crate)`). | ||
2 | |||
3 | use hir_expand::{hygiene::Hygiene, InFile}; | ||
4 | use ra_syntax::ast; | ||
5 | |||
6 | use crate::{ | ||
7 | db::DefDatabase, | ||
8 | path::{ModPath, PathKind}, | ||
9 | ModuleId, | ||
10 | }; | ||
11 | |||
12 | /// Visibility of an item, not yet resolved. | ||
13 | #[derive(Debug, Clone, PartialEq, Eq)] | ||
14 | pub enum RawVisibility { | ||
15 | /// `pub(in module)`, `pub(crate)` or `pub(super)`. Also private, which is | ||
16 | /// equivalent to `pub(self)`. | ||
17 | Module(ModPath), | ||
18 | /// `pub`. | ||
19 | Public, | ||
20 | } | ||
21 | |||
22 | impl RawVisibility { | ||
23 | const fn private() -> RawVisibility { | ||
24 | let path = ModPath { kind: PathKind::Super(0), segments: Vec::new() }; | ||
25 | RawVisibility::Module(path) | ||
26 | } | ||
27 | |||
28 | pub(crate) fn from_ast( | ||
29 | db: &impl DefDatabase, | ||
30 | node: InFile<Option<ast::Visibility>>, | ||
31 | ) -> RawVisibility { | ||
32 | Self::from_ast_with_hygiene(node.value, &Hygiene::new(db, node.file_id)) | ||
33 | } | ||
34 | |||
35 | pub(crate) fn from_ast_with_hygiene( | ||
36 | node: Option<ast::Visibility>, | ||
37 | hygiene: &Hygiene, | ||
38 | ) -> RawVisibility { | ||
39 | let node = match node { | ||
40 | None => return RawVisibility::private(), | ||
41 | Some(node) => node, | ||
42 | }; | ||
43 | match node.kind() { | ||
44 | ast::VisibilityKind::In(path) => { | ||
45 | let path = ModPath::from_src(path, hygiene); | ||
46 | let path = match path { | ||
47 | None => return RawVisibility::private(), | ||
48 | Some(path) => path, | ||
49 | }; | ||
50 | RawVisibility::Module(path) | ||
51 | } | ||
52 | ast::VisibilityKind::PubCrate => { | ||
53 | let path = ModPath { kind: PathKind::Crate, segments: Vec::new() }; | ||
54 | RawVisibility::Module(path) | ||
55 | } | ||
56 | ast::VisibilityKind::PubSuper => { | ||
57 | let path = ModPath { kind: PathKind::Super(1), segments: Vec::new() }; | ||
58 | RawVisibility::Module(path) | ||
59 | } | ||
60 | ast::VisibilityKind::Pub => RawVisibility::Public, | ||
61 | } | ||
62 | } | ||
63 | |||
64 | pub fn resolve( | ||
65 | &self, | ||
66 | db: &impl DefDatabase, | ||
67 | resolver: &crate::resolver::Resolver, | ||
68 | ) -> Visibility { | ||
69 | // we fall back to public visibility (i.e. fail open) if the path can't be resolved | ||
70 | resolver.resolve_visibility(db, self).unwrap_or(Visibility::Public) | ||
71 | } | ||
72 | } | ||
73 | |||
74 | /// Visibility of an item, with the path resolved. | ||
75 | #[derive(Debug, Copy, Clone, PartialEq, Eq)] | ||
76 | pub enum Visibility { | ||
77 | /// Visibility is restricted to a certain module. | ||
78 | Module(ModuleId), | ||
79 | /// Visibility is unrestricted. | ||
80 | Public, | ||
81 | } | ||
82 | |||
83 | impl Visibility { | ||
84 | pub fn is_visible_from(self, db: &impl DefDatabase, from_module: ModuleId) -> bool { | ||
85 | let to_module = match self { | ||
86 | Visibility::Module(m) => m, | ||
87 | Visibility::Public => return true, | ||
88 | }; | ||
89 | // if they're not in the same crate, it can't be visible | ||
90 | if from_module.krate != to_module.krate { | ||
91 | return false; | ||
92 | } | ||
93 | let def_map = db.crate_def_map(from_module.krate); | ||
94 | self.is_visible_from_def_map(&def_map, from_module.local_id) | ||
95 | } | ||
96 | |||
97 | pub(crate) fn is_visible_from_other_crate(self) -> bool { | ||
98 | match self { | ||
99 | Visibility::Module(_) => false, | ||
100 | Visibility::Public => true, | ||
101 | } | ||
102 | } | ||
103 | |||
104 | pub(crate) fn is_visible_from_def_map( | ||
105 | self, | ||
106 | def_map: &crate::nameres::CrateDefMap, | ||
107 | from_module: crate::LocalModuleId, | ||
108 | ) -> bool { | ||
109 | let to_module = match self { | ||
110 | Visibility::Module(m) => m, | ||
111 | Visibility::Public => return true, | ||
112 | }; | ||
113 | // from_module needs to be a descendant of to_module | ||
114 | let mut ancestors = std::iter::successors(Some(from_module), |m| { | ||
115 | let parent_id = def_map[*m].parent?; | ||
116 | Some(parent_id) | ||
117 | }); | ||
118 | ancestors.any(|m| m == to_module.local_id) | ||
119 | } | ||
120 | } | ||