2016-04-15 09:34:48 -05:00
|
|
|
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
use rustc::middle::privacy::{AccessLevels, AccessLevel};
|
|
|
|
use rustc::hir::def::Def;
|
2016-08-31 06:00:29 -05:00
|
|
|
use rustc::hir::def_id::{CrateNum, CRATE_DEF_INDEX, DefId};
|
2016-04-15 09:34:48 -05:00
|
|
|
use rustc::ty::Visibility;
|
2017-04-09 11:00:34 -05:00
|
|
|
use rustc::util::nodemap::FxHashSet;
|
2016-04-15 09:34:48 -05:00
|
|
|
|
|
|
|
use std::cell::RefMut;
|
|
|
|
|
2016-11-23 17:40:52 -06:00
|
|
|
use clean::{AttributesExt, NestedAttributesExt};
|
2016-04-15 09:34:48 -05:00
|
|
|
|
2016-04-17 01:54:48 -05:00
|
|
|
// FIXME: this may not be exhaustive, but is sufficient for rustdocs current uses
|
2016-04-15 09:34:48 -05:00
|
|
|
|
|
|
|
/// Similar to `librustc_privacy::EmbargoVisitor`, but also takes
|
|
|
|
/// specific rustdoc annotations into account (i.e. `doc(hidden)`)
|
2017-12-28 10:52:40 -06:00
|
|
|
pub struct LibEmbargoVisitor<'a, 'tcx: 'a, 'rcx: 'a> {
|
|
|
|
cx: &'a ::core::DocContext<'a, 'tcx, 'rcx>,
|
2016-04-15 09:34:48 -05:00
|
|
|
// Accessibility levels for reachable nodes
|
|
|
|
access_levels: RefMut<'a, AccessLevels<DefId>>,
|
|
|
|
// Previous accessibility level, None means unreachable
|
|
|
|
prev_level: Option<AccessLevel>,
|
2017-04-09 11:00:34 -05:00
|
|
|
// Keeps track of already visited modules, in case a module re-exports its parent
|
|
|
|
visited_mods: FxHashSet<DefId>,
|
2016-04-15 09:34:48 -05:00
|
|
|
}
|
|
|
|
|
2017-12-28 10:52:40 -06:00
|
|
|
impl<'a, 'tcx, 'rcx> LibEmbargoVisitor<'a, 'tcx, 'rcx> {
|
|
|
|
pub fn new(cx: &'a ::core::DocContext<'a, 'tcx, 'rcx>) -> LibEmbargoVisitor<'a, 'tcx, 'rcx> {
|
2016-04-15 09:34:48 -05:00
|
|
|
LibEmbargoVisitor {
|
2017-08-07 00:54:09 -05:00
|
|
|
cx,
|
2016-04-15 09:34:48 -05:00
|
|
|
access_levels: cx.access_levels.borrow_mut(),
|
|
|
|
prev_level: Some(AccessLevel::Public),
|
2017-04-09 11:00:34 -05:00
|
|
|
visited_mods: FxHashSet()
|
2016-04-15 09:34:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-31 06:00:29 -05:00
|
|
|
pub fn visit_lib(&mut self, cnum: CrateNum) {
|
2016-04-15 09:34:48 -05:00
|
|
|
let did = DefId { krate: cnum, index: CRATE_DEF_INDEX };
|
2016-04-25 01:24:50 -05:00
|
|
|
self.update(did, Some(AccessLevel::Public));
|
2016-04-15 09:34:48 -05:00
|
|
|
self.visit_mod(did);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Updates node level and returns the updated level
|
|
|
|
fn update(&mut self, did: DefId, level: Option<AccessLevel>) -> Option<AccessLevel> {
|
2016-11-19 19:42:54 -06:00
|
|
|
let is_hidden = self.cx.tcx.get_attrs(did).lists("doc").has_word("hidden");
|
2016-04-15 09:34:48 -05:00
|
|
|
|
|
|
|
let old_level = self.access_levels.map.get(&did).cloned();
|
|
|
|
// Accessibility levels can only grow
|
|
|
|
if level > old_level && !is_hidden {
|
|
|
|
self.access_levels.map.insert(did, level.unwrap());
|
|
|
|
level
|
|
|
|
} else {
|
|
|
|
old_level
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-15 03:05:45 -05:00
|
|
|
pub fn visit_mod(&mut self, def_id: DefId) {
|
2017-04-09 11:00:34 -05:00
|
|
|
if !self.visited_mods.insert(def_id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-31 10:07:39 -05:00
|
|
|
for item in self.cx.tcx.item_children(def_id).iter() {
|
2017-12-04 23:17:42 -06:00
|
|
|
if !item.is_import || item.vis == Visibility::Public {
|
|
|
|
self.visit_item(item.def);
|
|
|
|
}
|
2016-04-15 09:34:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-14 16:51:46 -05:00
|
|
|
fn visit_item(&mut self, def: Def) {
|
|
|
|
let def_id = def.def_id();
|
2017-08-31 10:07:39 -05:00
|
|
|
let vis = self.cx.tcx.visibility(def_id);
|
2016-09-15 03:05:45 -05:00
|
|
|
let inherited_item_level = if vis == Visibility::Public {
|
2016-09-08 11:05:50 -05:00
|
|
|
self.prev_level
|
|
|
|
} else {
|
|
|
|
None
|
2016-04-15 09:34:48 -05:00
|
|
|
};
|
|
|
|
|
2016-09-15 03:05:45 -05:00
|
|
|
let item_level = self.update(def_id, inherited_item_level);
|
2016-04-15 09:34:48 -05:00
|
|
|
|
2016-09-14 16:51:46 -05:00
|
|
|
if let Def::Mod(..) = def {
|
2016-04-15 09:34:48 -05:00
|
|
|
let orig_level = self.prev_level;
|
|
|
|
|
|
|
|
self.prev_level = item_level;
|
2016-09-15 03:05:45 -05:00
|
|
|
self.visit_mod(def_id);
|
2016-04-15 09:34:48 -05:00
|
|
|
self.prev_level = orig_level;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|