use std::collections::BTreeSet;
use std::ops::Bound;
use super::item_head::Visibility;
use super::path_vocab::{names_a_block_item, path_within};
use super::resolve::{
BindingNames, CrateScopes, Namespace, ReadingSize, Walk, extern_crate_names, least,
};
use super::scope_tree::{Binding, DeclKind};
pub(super) fn glob_reaches_prefix(
scopes: &CrateScopes,
glob: &str,
prefix: &str,
viewer: &str,
) -> Result<bool, String> {
let mut hazard = Hazard::default();
let mut visited: BTreeSet<String> = BTreeSet::new();
let (mut work, terminal_reaches) = scopes.hazard_paths(glob, Namespace::Type, prefix, true)?;
hazard.reaches |= terminal_reaches;
for path in &work {
hazard.work_size.add(path)?;
}
while let Some(glob) = work.pop() {
if !visited.insert(glob.clone()) {
continue;
}
if path_within(&glob, prefix) || path_within(prefix, &glob) {
hazard.reaches = true;
continue;
}
let beneath = scopes
.modules
.range::<str, _>((Bound::Included(glob.as_str()), Bound::Unbounded))
.take_while(|(module, _)| module.starts_with(glob.as_str()));
for (module, module_scopes) in beneath {
if !path_within(module, &glob) || names_a_block_item(module) {
continue;
}
for &(t, s) in module_scopes {
read_scope(
scopes,
t,
s,
&Reading {
prefix,
target: &glob,
viewer,
},
&mut hazard,
&mut work,
);
}
}
}
match hazard.refused {
Some(refusal) => Err(refusal),
None => Ok(hazard.reaches),
}
}
struct Reading<'a> {
prefix: &'a str,
target: &'a str,
viewer: &'a str,
}
#[derive(Default)]
struct Hazard {
reaches: bool,
refused: Option<String>,
work_size: ReadingSize,
}
impl Hazard {
fn refuse(&mut self, refusal: String) {
self.refused = least(self.refused.take(), refusal);
}
fn read(&mut self, denoted: Result<(Vec<String>, bool), String>, prefix: &str) {
match denoted {
Ok((found, terminal_reaches)) => {
self.reaches |= terminal_reaches || found.iter().any(|p| path_within(p, prefix))
}
Err(refusal) => self.refuse(refusal),
}
}
}
fn read_scope(
scopes: &CrateScopes,
t: usize,
s: u32,
reading: &Reading<'_>,
hazard: &mut Hazard,
work: &mut Vec<String>,
) {
read_reexports(scopes, t, s, reading, hazard);
read_extern_crates(scopes, t, s, reading, hazard);
queue_globs(scopes, t, s, reading, hazard, work);
}
fn read_reexports(
scopes: &CrateScopes,
t: usize,
s: u32,
reading: &Reading<'_>,
hazard: &mut Hazard,
) {
let scope = &scopes.tables[t].scopes[s as usize];
for (name, bindings) in &scope.bindings {
for binding in bindings {
let reexported = match binding {
Binding::Import { visibility, .. } => *visibility != Visibility::Private,
Binding::Alias { .. } => true,
};
if !reexported {
continue;
}
match scopes.binding_names(t, s, name, binding, Namespace::Either, &mut Walk::default())
{
BindingNames::Paths(paths, _) => {
for path in paths {
hazard.read(
scopes.hazard_paths(&path, Namespace::Either, reading.prefix, false),
reading.prefix,
);
}
}
BindingNames::Local => {}
BindingNames::PastCap(refusal) => hazard.refuse(refusal),
}
}
}
}
fn read_extern_crates(
scopes: &CrateScopes,
t: usize,
s: u32,
reading: &Reading<'_>,
hazard: &mut Hazard,
) {
let scope = &scopes.tables[t].scopes[s as usize];
for declaration in scope.declarations.values().flatten() {
if let DeclKind::ExternCrate { target, .. } = &declaration.kind {
if declaration.visibility != Visibility::Private {
hazard.read(
scopes.hazard_paths(
extern_crate_names(target).path(),
Namespace::Either,
reading.prefix,
false,
),
reading.prefix,
);
}
}
}
}
fn queue_globs(
scopes: &CrateScopes,
t: usize,
s: u32,
reading: &Reading<'_>,
hazard: &mut Hazard,
work: &mut Vec<String>,
) {
let scope = &scopes.tables[t].scopes[s as usize];
for (i, inner) in scope.globs.iter().enumerate() {
if inner.visibility != Visibility::Private
|| (scope.module == reading.target
&& inner.visibility.visible_from(&scope.module, reading.viewer))
{
match scopes.glob_targets(t, s, i) {
Ok(targets) => {
for target in targets {
if let Err(refusal) = hazard.work_size.add(&target) {
hazard.refuse(refusal);
break;
}
work.push(target);
}
}
Err(refusal) => hazard.refuse(refusal),
}
}
}
}