]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/privacy.rs
Auto merge of #31077 - nagisa:mir-temp-promotion, r=dotdash
[rust.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 pub use self::PrivateDep::*;
16 pub use self::ImportUse::*;
17 pub use self::LastPrivate::*;
18
19 use middle::def_id::DefId;
20 use util::nodemap::{DefIdSet, FnvHashMap};
21
22 use std::hash::Hash;
23 use syntax::ast::NodeId;
24
25 // Accessibility levels, sorted in ascending order
26 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
27 pub enum AccessLevel {
28     // Exported items + items participating in various kinds of public interfaces,
29     // but not directly nameable. For example, if function `fn f() -> T {...}` is
30     // public, then type `T` is exported. Its values can be obtained by other crates
31     // even if the type itseld is not nameable.
32     // FIXME: Mostly unimplemented. Only `type` aliases export items currently.
33     Reachable,
34     // Public items + items accessible to other crates with help of `pub use` reexports
35     Exported,
36     // Items accessible to other crates directly, without help of reexports
37     Public,
38 }
39
40 // Accessibility levels for reachable HIR nodes
41 #[derive(Clone)]
42 pub struct AccessLevels<Id = NodeId> {
43     pub map: FnvHashMap<Id, AccessLevel>
44 }
45
46 impl<Id: Hash + Eq> AccessLevels<Id> {
47     pub fn is_reachable(&self, id: Id) -> bool {
48         self.map.contains_key(&id)
49     }
50     pub fn is_exported(&self, id: Id) -> bool {
51         self.map.get(&id) >= Some(&AccessLevel::Exported)
52     }
53     pub fn is_public(&self, id: Id) -> bool {
54         self.map.get(&id) >= Some(&AccessLevel::Public)
55     }
56 }
57
58 impl<Id: Hash + Eq> Default for AccessLevels<Id> {
59     fn default() -> Self {
60         AccessLevels { map: Default::default() }
61     }
62 }
63
64 /// A set containing all exported definitions from external crates.
65 /// The set does not contain any entries from local crates.
66 pub type ExternalExports = DefIdSet;
67
68 #[derive(Copy, Clone, Debug)]
69 pub enum LastPrivate {
70     LastMod(PrivateDep),
71     // `use` directives (imports) can refer to two separate definitions in the
72     // type and value namespaces. We record here the last private node for each
73     // and whether the import is in fact used for each.
74     // If the Option<PrivateDep> fields are None, it means there is no definition
75     // in that namespace.
76     LastImport{value_priv: Option<PrivateDep>,
77                value_used: ImportUse,
78                type_priv: Option<PrivateDep>,
79                type_used: ImportUse},
80 }
81
82 #[derive(Copy, Clone, Debug)]
83 pub enum PrivateDep {
84     AllPublic,
85     DependsOn(DefId),
86 }
87
88 // How an import is used.
89 #[derive(Copy, Clone, PartialEq, Debug)]
90 pub enum ImportUse {
91     Unused,       // The import is not used.
92     Used,         // The import is used.
93 }
94
95 impl LastPrivate {
96     pub fn or(self, other: LastPrivate) -> LastPrivate {
97         match (self, other) {
98             (me, LastMod(AllPublic)) => me,
99             (_, other) => other,
100         }
101     }
102 }