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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//! A pass that checks to make sure private fields and methods aren't used
//! outside their scopes. This pass will also generate a set of exported items
//! which are available for use externally when compiled as a library.
use crate::ty::{DefIdTree, Visibility};
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_macros::HashStable;
use rustc_query_system::ich::StableHashingContext;
use rustc_span::def_id::{DefId, LocalDefId};
use std::hash::Hash;

/// Represents the levels of accessibility an item can have.
///
/// The variants are sorted in ascending order of accessibility.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, HashStable)]
pub enum AccessLevel {
    /// Superset of `AccessLevel::Reachable` used to mark impl Trait items.
    ReachableFromImplTrait,
    /// Exported items + items participating in various kinds of public interfaces,
    /// but not directly nameable. For example, if function `fn f() -> T {...}` is
    /// public, then type `T` is reachable. Its values can be obtained by other crates
    /// even if the type itself is not nameable.
    Reachable,
    /// Public items + items accessible to other crates with the help of `pub use` re-exports.
    Exported,
    /// Items accessible to other crates directly, without the help of re-exports.
    Public,
}

impl AccessLevel {
    pub fn all_levels() -> [AccessLevel; 4] {
        [
            AccessLevel::Public,
            AccessLevel::Exported,
            AccessLevel::Reachable,
            AccessLevel::ReachableFromImplTrait,
        ]
    }
}

#[derive(Clone, Copy, PartialEq, Eq, Debug, HashStable)]
pub struct EffectiveVisibility {
    public: Visibility,
    exported: Visibility,
    reachable: Visibility,
    reachable_from_impl_trait: Visibility,
}

impl EffectiveVisibility {
    pub fn get(&self, tag: AccessLevel) -> &Visibility {
        match tag {
            AccessLevel::Public => &self.public,
            AccessLevel::Exported => &self.exported,
            AccessLevel::Reachable => &self.reachable,
            AccessLevel::ReachableFromImplTrait => &self.reachable_from_impl_trait,
        }
    }

    fn get_mut(&mut self, tag: AccessLevel) -> &mut Visibility {
        match tag {
            AccessLevel::Public => &mut self.public,
            AccessLevel::Exported => &mut self.exported,
            AccessLevel::Reachable => &mut self.reachable,
            AccessLevel::ReachableFromImplTrait => &mut self.reachable_from_impl_trait,
        }
    }

    pub fn is_public_at_level(&self, tag: AccessLevel) -> bool {
        self.get(tag).is_public()
    }

    fn update(&mut self, vis: Visibility, tag: AccessLevel, tree: impl DefIdTree) -> bool {
        let mut changed = false;
        for level in AccessLevel::all_levels() {
            if level <= tag {
                let current_effective_vis = self.get_mut(level);
                if *current_effective_vis != vis && vis.is_at_least(*current_effective_vis, tree) {
                    changed = true;
                    *current_effective_vis = vis;
                }
            }
        }
        changed
    }

    fn from_vis(vis: Visibility) -> EffectiveVisibility {
        EffectiveVisibility {
            public: vis,
            exported: vis,
            reachable: vis,
            reachable_from_impl_trait: vis,
        }
    }
}

/// Holds a map of accessibility levels for reachable HIR nodes.
#[derive(Debug, Clone)]
pub struct AccessLevels<Id = LocalDefId> {
    map: FxHashMap<Id, EffectiveVisibility>,
}

impl<Id: Hash + Eq + Copy> AccessLevels<Id> {
    pub fn is_public_at_level(&self, id: Id, tag: AccessLevel) -> bool {
        self.get_effective_vis(id)
            .map_or(false, |effective_vis| effective_vis.is_public_at_level(tag))
    }

    /// See `AccessLevel::Reachable`.
    pub fn is_reachable(&self, id: Id) -> bool {
        self.is_public_at_level(id, AccessLevel::Reachable)
    }

    /// See `AccessLevel::Exported`.
    pub fn is_exported(&self, id: Id) -> bool {
        self.is_public_at_level(id, AccessLevel::Exported)
    }

    /// See `AccessLevel::Public`.
    pub fn is_public(&self, id: Id) -> bool {
        self.is_public_at_level(id, AccessLevel::Public)
    }

    pub fn get_access_level(&self, id: Id) -> Option<AccessLevel> {
        self.get_effective_vis(id).and_then(|effective_vis| {
            for level in AccessLevel::all_levels() {
                if effective_vis.is_public_at_level(level) {
                    return Some(level);
                }
            }
            None
        })
    }

    pub fn get_effective_vis(&self, id: Id) -> Option<&EffectiveVisibility> {
        self.map.get(&id)
    }

    pub fn iter(&self) -> impl Iterator<Item = (&Id, &EffectiveVisibility)> {
        self.map.iter()
    }

    pub fn map_id<OutId: Hash + Eq + Copy>(&self, f: impl Fn(Id) -> OutId) -> AccessLevels<OutId> {
        AccessLevels { map: self.map.iter().map(|(k, v)| (f(*k), *v)).collect() }
    }

    pub fn set_access_level(
        &mut self,
        id: Id,
        default_vis: impl FnOnce() -> Visibility,
        tag: AccessLevel,
    ) {
        let mut effective_vis = self
            .get_effective_vis(id)
            .copied()
            .unwrap_or_else(|| EffectiveVisibility::from_vis(default_vis()));
        for level in AccessLevel::all_levels() {
            if level <= tag {
                *effective_vis.get_mut(level) = Visibility::Public;
            }
        }
        self.map.insert(id, effective_vis);
    }
}

impl<Id: Hash + Eq + Copy + Into<DefId>> AccessLevels<Id> {
    // `parent_id` is not necessarily a parent in source code tree,
    // it is the node from which the maximum effective visibility is inherited.
    pub fn update(
        &mut self,
        id: Id,
        nominal_vis: Visibility,
        default_vis: impl FnOnce() -> Visibility,
        parent_id: Id,
        tag: AccessLevel,
        tree: impl DefIdTree,
    ) -> Result<bool, ()> {
        let mut changed = false;
        let mut current_effective_vis = self
            .get_effective_vis(id)
            .copied()
            .unwrap_or_else(|| EffectiveVisibility::from_vis(default_vis()));
        if let Some(inherited_effective_vis) = self.get_effective_vis(parent_id) {
            for level in AccessLevel::all_levels() {
                if tag >= level {
                    let inherited_effective_vis_at_level = *inherited_effective_vis.get(level);
                    let calculated_effective_vis =
                        if nominal_vis.is_at_least(inherited_effective_vis_at_level, tree) {
                            inherited_effective_vis_at_level
                        } else {
                            nominal_vis
                        };
                    changed |= current_effective_vis.update(calculated_effective_vis, level, tree);
                }
            }
        } else {
            if !id.into().is_crate_root() {
                return Err(());
            }
            changed |= current_effective_vis.update(Visibility::Public, AccessLevel::Public, tree);
        }
        self.map.insert(id, current_effective_vis);
        Ok(changed)
    }
}

impl<Id> Default for AccessLevels<Id> {
    fn default() -> Self {
        AccessLevels { map: Default::default() }
    }
}

impl<'a> HashStable<StableHashingContext<'a>> for AccessLevels {
    fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
        let AccessLevels { ref map } = *self;
        map.hash_stable(hcx, hasher);
    }
}