]> git.proxmox.com Git - rustc.git/blob - src/librustc/middle/privacy.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.git] / src / librustc / middle / privacy.rs
1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! A pass that checks to make sure private fields and methods aren't used
12 //! outside their scopes. This pass will also generate a set of exported items
13 //! which are available for use externally when compiled as a library.
14
15 use util::nodemap::{DefIdSet, FnvHashMap};
16
17 use std::hash::Hash;
18 use syntax::ast::NodeId;
19
20 // Accessibility levels, sorted in ascending order
21 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
22 pub enum AccessLevel {
23 // Exported items + items participating in various kinds of public interfaces,
24 // but not directly nameable. For example, if function `fn f() -> T {...}` is
25 // public, then type `T` is exported. Its values can be obtained by other crates
26 // even if the type itseld is not nameable.
27 // FIXME: Mostly unimplemented. Only `type` aliases export items currently.
28 Reachable,
29 // Public items + items accessible to other crates with help of `pub use` reexports
30 Exported,
31 // Items accessible to other crates directly, without help of reexports
32 Public,
33 }
34
35 // Accessibility levels for reachable HIR nodes
36 #[derive(Clone)]
37 pub struct AccessLevels<Id = NodeId> {
38 pub map: FnvHashMap<Id, AccessLevel>
39 }
40
41 impl<Id: Hash + Eq> AccessLevels<Id> {
42 pub fn is_reachable(&self, id: Id) -> bool {
43 self.map.contains_key(&id)
44 }
45 pub fn is_exported(&self, id: Id) -> bool {
46 self.map.get(&id) >= Some(&AccessLevel::Exported)
47 }
48 pub fn is_public(&self, id: Id) -> bool {
49 self.map.get(&id) >= Some(&AccessLevel::Public)
50 }
51 }
52
53 impl<Id: Hash + Eq> Default for AccessLevels<Id> {
54 fn default() -> Self {
55 AccessLevels { map: Default::default() }
56 }
57 }
58
59 /// A set containing all exported definitions from external crates.
60 /// The set does not contain any entries from local crates.
61 pub type ExternalExports = DefIdSet;