use std::iter;
use intern::Interned;
use la_arena::ArenaMap;
use span::SyntaxContextId;
use syntax::ast;
use triomphe::Arc;
use crate::{
db::DefDatabase,
nameres::DefMap,
path::{ModPath, PathKind},
resolver::HasResolver,
ConstId, FunctionId, HasModule, LocalFieldId, LocalModuleId, ModuleId, VariantId,
};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RawVisibility {
Module(Interned<ModPath>, VisibilityExplicitness),
Public,
}
impl RawVisibility {
pub(crate) fn private() -> RawVisibility {
RawVisibility::Module(
Interned::new(ModPath::from_kind(PathKind::SELF)),
VisibilityExplicitness::Implicit,
)
}
pub(crate) fn from_ast(
db: &dyn DefDatabase,
node: Option<ast::Visibility>,
span_for_range: &mut dyn FnMut(::tt::TextRange) -> SyntaxContextId,
) -> RawVisibility {
let node = match node {
None => return RawVisibility::private(),
Some(node) => node,
};
Self::from_ast_with_span_map(db, node, span_for_range)
}
fn from_ast_with_span_map(
db: &dyn DefDatabase,
node: ast::Visibility,
span_for_range: &mut dyn FnMut(::tt::TextRange) -> SyntaxContextId,
) -> RawVisibility {
let path = match node.kind() {
ast::VisibilityKind::In(path) => {
let path = ModPath::from_src(db.upcast(), path, span_for_range);
match path {
None => return RawVisibility::private(),
Some(path) => path,
}
}
ast::VisibilityKind::PubCrate => ModPath::from_kind(PathKind::Crate),
ast::VisibilityKind::PubSuper => ModPath::from_kind(PathKind::Super(1)),
ast::VisibilityKind::PubSelf => ModPath::from_kind(PathKind::SELF),
ast::VisibilityKind::Pub => return RawVisibility::Public,
};
RawVisibility::Module(Interned::new(path), VisibilityExplicitness::Explicit)
}
pub fn resolve(
&self,
db: &dyn DefDatabase,
resolver: &crate::resolver::Resolver,
) -> Visibility {
resolver.resolve_visibility(db, self).unwrap_or(Visibility::Public)
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum Visibility {
Module(ModuleId, VisibilityExplicitness),
Public,
}
impl Visibility {
pub(crate) fn is_visible_from_other_crate(self) -> bool {
matches!(self, Visibility::Public)
}
#[tracing::instrument(skip_all)]
pub fn is_visible_from(self, db: &dyn DefDatabase, from_module: ModuleId) -> bool {
let to_module = match self {
Visibility::Module(m, _) => m,
Visibility::Public => return true,
};
if from_module.krate != to_module.krate {
return false;
}
let def_map = from_module.def_map(db);
Self::is_visible_from_def_map_(db, &def_map, to_module, from_module.local_id)
}
pub(crate) fn is_visible_from_def_map(
self,
db: &dyn DefDatabase,
def_map: &DefMap,
from_module: LocalModuleId,
) -> bool {
let to_module = match self {
Visibility::Module(m, _) => m,
Visibility::Public => return true,
};
if def_map.krate() != to_module.krate {
return false;
}
Self::is_visible_from_def_map_(db, def_map, to_module, from_module)
}
fn is_visible_from_def_map_(
db: &dyn DefDatabase,
def_map: &DefMap,
mut to_module: ModuleId,
mut from_module: LocalModuleId,
) -> bool {
debug_assert_eq!(to_module.krate, def_map.krate());
let def_map_block = def_map.block_id();
loop {
match (to_module.block, def_map_block) {
(None, _) => (),
(Some(a), Some(b)) if a == b => {
cov_mark::hit!(is_visible_from_same_block_def_map);
}
_ => {
if let Some(parent) = to_module.def_map(db).parent() {
to_module = parent;
continue;
}
}
}
break;
}
let mut def_map = def_map;
let mut parent_arc;
loop {
if def_map.module_id(from_module) == to_module {
return true;
}
match def_map[from_module].parent {
Some(parent) => from_module = parent,
None => {
match def_map.parent() {
Some(module) => {
parent_arc = module.def_map(db);
def_map = &*parent_arc;
from_module = module.local_id;
}
None => return false,
}
}
}
}
}
pub(crate) fn max(self, other: Visibility, def_map: &DefMap) -> Option<Visibility> {
match (self, other) {
(_, Visibility::Public) | (Visibility::Public, _) => Some(Visibility::Public),
(Visibility::Module(mod_a, expl_a), Visibility::Module(mod_b, expl_b)) => {
if mod_a.krate != mod_b.krate {
return None;
}
let def_block = def_map.block_id();
if (mod_a.containing_block(), mod_b.containing_block()) != (def_block, def_block) {
return None;
}
let mut a_ancestors =
iter::successors(Some(mod_a.local_id), |&m| def_map[m].parent);
let mut b_ancestors =
iter::successors(Some(mod_b.local_id), |&m| def_map[m].parent);
if a_ancestors.any(|m| m == mod_b.local_id) {
return Some(Visibility::Module(mod_b, expl_b));
}
if b_ancestors.any(|m| m == mod_a.local_id) {
return Some(Visibility::Module(mod_a, expl_a));
}
None
}
}
}
pub(crate) fn min(self, other: Visibility, def_map: &DefMap) -> Option<Visibility> {
match (self, other) {
(vis, Visibility::Public) | (Visibility::Public, vis) => Some(vis),
(Visibility::Module(mod_a, expl_a), Visibility::Module(mod_b, expl_b)) => {
if mod_a.krate != mod_b.krate {
return None;
}
let def_block = def_map.block_id();
if (mod_a.containing_block(), mod_b.containing_block()) != (def_block, def_block) {
return None;
}
let mut a_ancestors =
iter::successors(Some(mod_a.local_id), |&m| def_map[m].parent);
let mut b_ancestors =
iter::successors(Some(mod_b.local_id), |&m| def_map[m].parent);
if a_ancestors.any(|m| m == mod_b.local_id) {
return Some(Visibility::Module(mod_a, expl_b));
}
if b_ancestors.any(|m| m == mod_a.local_id) {
return Some(Visibility::Module(mod_b, expl_a));
}
None
}
}
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum VisibilityExplicitness {
Explicit,
Implicit,
}
impl VisibilityExplicitness {
pub fn is_explicit(&self) -> bool {
matches!(self, Self::Explicit)
}
}
pub(crate) fn field_visibilities_query(
db: &dyn DefDatabase,
variant_id: VariantId,
) -> Arc<ArenaMap<LocalFieldId, Visibility>> {
let var_data = variant_id.variant_data(db);
let resolver = variant_id.module(db).resolver(db);
let mut res = ArenaMap::default();
for (field_id, field_data) in var_data.fields().iter() {
res.insert(field_id, field_data.visibility.resolve(db, &resolver));
}
Arc::new(res)
}
pub(crate) fn function_visibility_query(db: &dyn DefDatabase, def: FunctionId) -> Visibility {
let resolver = def.resolver(db);
db.function_data(def).visibility.resolve(db, &resolver)
}
pub(crate) fn const_visibility_query(db: &dyn DefDatabase, def: ConstId) -> Visibility {
let resolver = def.resolver(db);
db.const_data(def).visibility.resolve(db, &resolver)
}