|
| 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, ChildItem, DefLike}; |
| 12 | +use rustc::middle::privacy::{AccessLevels, AccessLevel}; |
| 13 | +use rustc::hir::def::Def; |
| 14 | +use rustc::hir::def_id::{CRATE_DEF_INDEX, DefId}; |
| 15 | +use rustc::ty::Visibility; |
| 16 | +use syntax::ast; |
| 17 | + |
| 18 | +use std::cell::RefMut; |
| 19 | + |
| 20 | +use clean::{Attributes, Clean}; |
| 21 | + |
| 22 | +// FIXME: since this is only used for cross-crate impl inlining this only |
| 23 | +// handles traits and items for which traits can be implemented |
| 24 | + |
| 25 | +/// Similar to `librustc_privacy::EmbargoVisitor`, but also takes |
| 26 | +/// specific rustdoc annotations into account (i.e. `doc(hidden)`) |
| 27 | +pub struct LibEmbargoVisitor<'a, 'b: 'a, 'tcx: 'b> { |
| 28 | + cx: &'a ::core::DocContext<'b, 'tcx>, |
| 29 | + cstore: &'a CrateStore<'tcx>, |
| 30 | + // Accessibility levels for reachable nodes |
| 31 | + access_levels: RefMut<'a, AccessLevels<DefId>>, |
| 32 | + // Previous accessibility level, None means unreachable |
| 33 | + prev_level: Option<AccessLevel>, |
| 34 | +} |
| 35 | + |
| 36 | +impl<'a, 'b, 'tcx> LibEmbargoVisitor<'a, 'b, 'tcx> { |
| 37 | + pub fn new(cx: &'a ::core::DocContext<'b, 'tcx>) -> LibEmbargoVisitor<'a, 'b, 'tcx> { |
| 38 | + LibEmbargoVisitor { |
| 39 | + cx: cx, |
| 40 | + cstore: &*cx.sess().cstore, |
| 41 | + access_levels: cx.access_levels.borrow_mut(), |
| 42 | + prev_level: Some(AccessLevel::Public), |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + pub fn visit_lib(&mut self, cnum: ast::CrateNum) { |
| 47 | + let did = DefId { krate: cnum, index: CRATE_DEF_INDEX }; |
| 48 | + self.visit_mod(did); |
| 49 | + } |
| 50 | + |
| 51 | + // Updates node level and returns the updated level |
| 52 | + fn update(&mut self, did: DefId, level: Option<AccessLevel>) -> Option<AccessLevel> { |
| 53 | + let attrs: Vec<_> = self.cx.tcx().get_attrs(did).iter() |
| 54 | + .map(|a| a.clean(self.cx)) |
| 55 | + .collect(); |
| 56 | + let is_hidden = attrs.list("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, did: DefId) { |
| 69 | + for item in self.cstore.item_children(did) { |
| 70 | + if let DefLike::DlDef(def) = item.def { |
| 71 | + match def { |
| 72 | + Def::Trait(did) | |
| 73 | + Def::Struct(did) | |
| 74 | + Def::Mod(did) | |
| 75 | + Def::Enum(did) | |
| 76 | + Def::TyAlias(did) => self.visit_item(did, item), |
| 77 | + _ => {} |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + fn visit_item(&mut self, did: DefId, item: ChildItem) { |
| 84 | + let inherited_item_level = match item.def { |
| 85 | + DefLike::DlImpl(..) | DefLike::DlField => unreachable!(), |
| 86 | + DefLike::DlDef(def) => { |
| 87 | + match def { |
| 88 | + Def::ForeignMod(..) => self.prev_level, |
| 89 | + _ => if item.vis == Visibility::Public { self.prev_level } else { None } |
| 90 | + } |
| 91 | + } |
| 92 | + }; |
| 93 | + |
| 94 | + let item_level = self.update(did, inherited_item_level); |
| 95 | + |
| 96 | + if let DefLike::DlDef(Def::Mod(did)) = item.def { |
| 97 | + let orig_level = self.prev_level; |
| 98 | + |
| 99 | + self.prev_level = item_level; |
| 100 | + self.visit_mod(did); |
| 101 | + self.prev_level = orig_level; |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments