]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/visit_lib.rs
Auto merge of #55798 - GuillaumeGomez:version-display-associated-const, r=QuietMisdreavus
[rust.git] / src / librustdoc / visit_lib.rs
1 // Copyright 2016 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 use rustc::middle::privacy::{AccessLevels, AccessLevel};
12 use rustc::hir::def::Def;
13 use rustc::hir::def_id::{CrateNum, CRATE_DEF_INDEX, DefId};
14 use rustc::ty::Visibility;
15 use rustc::util::nodemap::FxHashSet;
16
17 use std::cell::RefMut;
18
19 use clean::{AttributesExt, NestedAttributesExt};
20
21 // FIXME: this may not be exhaustive, but is sufficient for rustdocs current uses
22
23 /// Similar to `librustc_privacy::EmbargoVisitor`, but also takes
24 /// specific rustdoc annotations into account (i.e., `doc(hidden)`)
25 pub struct LibEmbargoVisitor<'a, 'tcx: 'a, 'rcx: 'a> {
26     cx: &'a ::core::DocContext<'a, 'tcx, 'rcx>,
27     // Accessibility levels for reachable nodes
28     access_levels: RefMut<'a, AccessLevels<DefId>>,
29     // Previous accessibility level, None means unreachable
30     prev_level: Option<AccessLevel>,
31     // Keeps track of already visited modules, in case a module re-exports its parent
32     visited_mods: FxHashSet<DefId>,
33 }
34
35 impl<'a, 'tcx, 'rcx> LibEmbargoVisitor<'a, 'tcx, 'rcx> {
36     pub fn new(
37         cx: &'a ::core::DocContext<'a, 'tcx, 'rcx>
38     ) -> LibEmbargoVisitor<'a, 'tcx, 'rcx> {
39         LibEmbargoVisitor {
40             cx,
41             access_levels: RefMut::map(cx.renderinfo.borrow_mut(), |ri| &mut ri.access_levels),
42             prev_level: Some(AccessLevel::Public),
43             visited_mods: FxHashSet::default()
44         }
45     }
46
47     pub fn visit_lib(&mut self, cnum: CrateNum) {
48         let did = DefId { krate: cnum, index: CRATE_DEF_INDEX };
49         self.update(did, Some(AccessLevel::Public));
50         self.visit_mod(did);
51     }
52
53     // Updates node level and returns the updated level
54     fn update(&mut self, did: DefId, level: Option<AccessLevel>) -> Option<AccessLevel> {
55         let is_hidden = self.cx.tcx.get_attrs(did).lists("doc").has_word("hidden");
56
57         let old_level = self.access_levels.map.get(&did).cloned();
58         // Accessibility levels can only grow
59         if level > old_level && !is_hidden {
60             self.access_levels.map.insert(did, level.unwrap());
61             level
62         } else {
63             old_level
64         }
65     }
66
67     pub fn visit_mod(&mut self, def_id: DefId) {
68         if !self.visited_mods.insert(def_id) {
69             return;
70         }
71
72         for item in self.cx.tcx.item_children(def_id).iter() {
73             if self.cx.tcx.def_key(item.def.def_id()).parent.map_or(false, |d| d == def_id.index) ||
74                 item.vis == Visibility::Public {
75                 self.visit_item(item.def);
76             }
77         }
78     }
79
80     fn visit_item(&mut self, def: Def) {
81         let def_id = def.def_id();
82         let vis = self.cx.tcx.visibility(def_id);
83         let inherited_item_level = if vis == Visibility::Public {
84             self.prev_level
85         } else {
86             None
87         };
88
89         let item_level = self.update(def_id, inherited_item_level);
90
91         if let Def::Mod(..) = def {
92             let orig_level = self.prev_level;
93
94             self.prev_level = item_level;
95             self.visit_mod(def_id);
96             self.prev_level = orig_level;
97         }
98     }
99 }