2019-12-24 15:11:50 -06:00
|
|
|
//! Defines hir-level representation of visibility (e.g. `pub` and `pub(crate)`).
|
|
|
|
|
2019-12-24 13:32:42 -06:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use either::Either;
|
|
|
|
|
2019-12-24 16:45:14 -06:00
|
|
|
use hir_expand::{hygiene::Hygiene, InFile};
|
2019-12-24 13:32:42 -06:00
|
|
|
use ra_syntax::ast::{self, VisibilityOwner};
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
db::DefDatabase,
|
|
|
|
path::{ModPath, PathKind},
|
|
|
|
src::{HasChildSource, HasSource},
|
2019-12-24 14:23:22 -06:00
|
|
|
AdtId, Lookup, ModuleId, VisibilityDefId,
|
2019-12-24 13:32:42 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Visibility of an item, not yet resolved.
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
2019-12-26 08:57:14 -06:00
|
|
|
pub enum RawVisibility {
|
2019-12-24 13:32:42 -06:00
|
|
|
// FIXME: We could avoid the allocation in many cases by special-casing
|
|
|
|
// pub(crate), pub(super) and private. Alternatively, `ModPath` could be
|
|
|
|
// made to contain an Arc<[Segment]> instead of a Vec?
|
|
|
|
/// `pub(in module)`, `pub(crate)` or `pub(super)`. Also private, which is
|
|
|
|
/// equivalent to `pub(self)`.
|
|
|
|
Module(Arc<ModPath>),
|
|
|
|
/// `pub`.
|
|
|
|
Public,
|
|
|
|
}
|
|
|
|
|
2019-12-26 08:57:14 -06:00
|
|
|
impl RawVisibility {
|
|
|
|
pub(crate) fn visibility_query(db: &impl DefDatabase, def: VisibilityDefId) -> RawVisibility {
|
2019-12-24 13:32:42 -06:00
|
|
|
match def {
|
|
|
|
VisibilityDefId::ModuleId(module) => {
|
|
|
|
let def_map = db.crate_def_map(module.krate);
|
|
|
|
let src = match def_map[module.local_id].declaration_source(db) {
|
|
|
|
Some(it) => it,
|
2019-12-26 08:57:14 -06:00
|
|
|
None => return RawVisibility::private(),
|
2019-12-24 13:32:42 -06:00
|
|
|
};
|
2019-12-26 08:57:14 -06:00
|
|
|
RawVisibility::from_ast(db, src.map(|it| it.visibility()))
|
2019-12-24 13:32:42 -06:00
|
|
|
}
|
|
|
|
VisibilityDefId::StructFieldId(it) => {
|
|
|
|
let src = it.parent.child_source(db);
|
2019-12-24 15:10:44 -06:00
|
|
|
let is_enum = match it.parent {
|
|
|
|
crate::VariantId::EnumVariantId(_) => true,
|
|
|
|
_ => false,
|
|
|
|
};
|
2019-12-24 13:32:42 -06:00
|
|
|
let vis_node = src.map(|m| match &m[it.local_id] {
|
|
|
|
Either::Left(tuple) => tuple.visibility(),
|
|
|
|
Either::Right(record) => record.visibility(),
|
|
|
|
});
|
2019-12-24 15:10:44 -06:00
|
|
|
if vis_node.value.is_none() && is_enum {
|
2019-12-26 08:57:14 -06:00
|
|
|
RawVisibility::Public
|
2019-12-24 15:10:44 -06:00
|
|
|
} else {
|
2019-12-26 08:57:14 -06:00
|
|
|
RawVisibility::from_ast(db, vis_node)
|
2019-12-24 15:10:44 -06:00
|
|
|
}
|
2019-12-24 13:32:42 -06:00
|
|
|
}
|
|
|
|
VisibilityDefId::AdtId(it) => match it {
|
|
|
|
AdtId::StructId(it) => visibility_from_loc(it.lookup(db), db),
|
|
|
|
AdtId::EnumId(it) => visibility_from_loc(it.lookup(db), db),
|
|
|
|
AdtId::UnionId(it) => visibility_from_loc(it.lookup(db), db),
|
|
|
|
},
|
|
|
|
VisibilityDefId::TraitId(it) => visibility_from_loc(it.lookup(db), db),
|
|
|
|
VisibilityDefId::ConstId(it) => visibility_from_loc(it.lookup(db), db),
|
|
|
|
VisibilityDefId::StaticId(it) => visibility_from_loc(it.lookup(db), db),
|
|
|
|
VisibilityDefId::FunctionId(it) => visibility_from_loc(it.lookup(db), db),
|
|
|
|
VisibilityDefId::TypeAliasId(it) => visibility_from_loc(it.lookup(db), db),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-26 08:57:14 -06:00
|
|
|
fn private() -> RawVisibility {
|
2019-12-24 13:32:42 -06:00
|
|
|
let path = ModPath { kind: PathKind::Super(0), segments: Vec::new() };
|
2019-12-26 08:57:14 -06:00
|
|
|
RawVisibility::Module(Arc::new(path))
|
2019-12-24 13:32:42 -06:00
|
|
|
}
|
|
|
|
|
2019-12-26 08:57:14 -06:00
|
|
|
fn from_ast(db: &impl DefDatabase, node: InFile<Option<ast::Visibility>>) -> RawVisibility {
|
2019-12-24 16:45:14 -06:00
|
|
|
Self::from_ast_with_hygiene(node.value, &Hygiene::new(db, node.file_id))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn from_ast_with_hygiene(
|
|
|
|
node: Option<ast::Visibility>,
|
|
|
|
hygiene: &Hygiene,
|
2019-12-26 08:57:14 -06:00
|
|
|
) -> RawVisibility {
|
2019-12-24 16:45:14 -06:00
|
|
|
let node = match node {
|
2019-12-26 08:57:14 -06:00
|
|
|
None => return RawVisibility::private(),
|
2019-12-24 13:32:42 -06:00
|
|
|
Some(node) => node,
|
|
|
|
};
|
|
|
|
match node.kind() {
|
|
|
|
ast::VisibilityKind::In(path) => {
|
2019-12-24 16:45:14 -06:00
|
|
|
let path = ModPath::from_src(path, hygiene);
|
2019-12-24 13:32:42 -06:00
|
|
|
let path = match path {
|
2019-12-26 08:57:14 -06:00
|
|
|
None => return RawVisibility::private(),
|
2019-12-24 13:32:42 -06:00
|
|
|
Some(path) => path,
|
|
|
|
};
|
2019-12-26 08:57:14 -06:00
|
|
|
RawVisibility::Module(Arc::new(path))
|
2019-12-24 13:32:42 -06:00
|
|
|
}
|
|
|
|
ast::VisibilityKind::PubCrate => {
|
|
|
|
let path = ModPath { kind: PathKind::Crate, segments: Vec::new() };
|
2019-12-26 08:57:14 -06:00
|
|
|
RawVisibility::Module(Arc::new(path))
|
2019-12-24 13:32:42 -06:00
|
|
|
}
|
|
|
|
ast::VisibilityKind::PubSuper => {
|
|
|
|
let path = ModPath { kind: PathKind::Super(1), segments: Vec::new() };
|
2019-12-26 08:57:14 -06:00
|
|
|
RawVisibility::Module(Arc::new(path))
|
2019-12-24 13:32:42 -06:00
|
|
|
}
|
2019-12-26 08:57:14 -06:00
|
|
|
ast::VisibilityKind::Pub => RawVisibility::Public,
|
2019-12-24 13:32:42 -06:00
|
|
|
}
|
|
|
|
}
|
2019-12-24 14:23:22 -06:00
|
|
|
|
|
|
|
pub fn resolve(
|
|
|
|
&self,
|
|
|
|
db: &impl DefDatabase,
|
|
|
|
resolver: &crate::resolver::Resolver,
|
2019-12-26 09:00:10 -06:00
|
|
|
) -> Visibility {
|
2019-12-24 14:23:22 -06:00
|
|
|
// we fall back to public visibility (i.e. fail open) if the path can't be resolved
|
2019-12-26 09:00:10 -06:00
|
|
|
resolver.resolve_visibility(db, self).unwrap_or(Visibility::Public)
|
2019-12-24 14:23:22 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Visibility of an item, with the path resolved.
|
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
2019-12-26 09:00:10 -06:00
|
|
|
pub enum Visibility {
|
2019-12-24 14:23:22 -06:00
|
|
|
/// Visibility is restricted to a certain module.
|
|
|
|
Module(ModuleId),
|
|
|
|
/// Visibility is unrestricted.
|
|
|
|
Public,
|
|
|
|
}
|
|
|
|
|
2019-12-26 09:00:10 -06:00
|
|
|
impl Visibility {
|
2019-12-24 14:23:22 -06:00
|
|
|
pub fn visible_from(self, db: &impl DefDatabase, from_module: ModuleId) -> bool {
|
|
|
|
let to_module = match self {
|
2019-12-26 09:00:10 -06:00
|
|
|
Visibility::Module(m) => m,
|
|
|
|
Visibility::Public => return true,
|
2019-12-24 14:23:22 -06:00
|
|
|
};
|
|
|
|
// if they're not in the same crate, it can't be visible
|
|
|
|
if from_module.krate != to_module.krate {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
let def_map = db.crate_def_map(from_module.krate);
|
2019-12-25 11:05:16 -06:00
|
|
|
self.visible_from_def_map(&def_map, from_module.local_id)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn visible_from_def_map(
|
|
|
|
self,
|
|
|
|
def_map: &crate::nameres::CrateDefMap,
|
|
|
|
from_module: crate::LocalModuleId,
|
|
|
|
) -> bool {
|
|
|
|
let to_module = match self {
|
2019-12-26 09:00:10 -06:00
|
|
|
Visibility::Module(m) => m,
|
|
|
|
Visibility::Public => return true,
|
2019-12-25 11:05:16 -06:00
|
|
|
};
|
|
|
|
// from_module needs to be a descendant of to_module
|
2019-12-24 14:23:22 -06:00
|
|
|
let mut ancestors = std::iter::successors(Some(from_module), |m| {
|
2019-12-25 11:05:16 -06:00
|
|
|
let parent_id = def_map[*m].parent?;
|
|
|
|
Some(parent_id)
|
2019-12-24 14:23:22 -06:00
|
|
|
});
|
2019-12-25 11:05:16 -06:00
|
|
|
ancestors.any(|m| m == to_module.local_id)
|
2019-12-24 14:23:22 -06:00
|
|
|
}
|
2019-12-24 13:32:42 -06:00
|
|
|
}
|
|
|
|
|
2019-12-26 08:57:14 -06:00
|
|
|
fn visibility_from_loc<T>(node: T, db: &impl DefDatabase) -> RawVisibility
|
2019-12-24 13:32:42 -06:00
|
|
|
where
|
|
|
|
T: HasSource,
|
|
|
|
T::Value: ast::VisibilityOwner,
|
|
|
|
{
|
|
|
|
let src = node.source(db);
|
2019-12-26 08:57:14 -06:00
|
|
|
RawVisibility::from_ast(db, src.map(|n| n.visibility()))
|
2019-12-24 13:32:42 -06:00
|
|
|
}
|