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