use std::collections::BTreeMap;
use super::item_head::block_modules;
use super::item_head::{
GroupKind, ItemKeyword, Visibility, classify_group, heads_a_path, item_at_keyword,
macro_group_kind, macro_stands_as_an_item, may_be_cfg_gated,
};
use super::occurrence::path_run;
use super::path_vocab::block_segment;
use super::token_tree::{Delimiter, Kind, Node, TokenTree};
use super::use_tree::{UseLeaf, macro_use_statements, use_statements};
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub(super) enum Namespace {
Type,
Value,
Either,
}
impl Namespace {
pub(super) fn other(self) -> Namespace {
match self {
Namespace::Type => Namespace::Value,
Namespace::Value => Namespace::Type,
Namespace::Either => Namespace::Either,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum ScopeKind {
Module,
Block,
}
pub(super) enum Binding {
Import {
written: String,
visibility: Visibility,
module_only: bool,
},
Alias {
written: String,
visibility: Visibility,
},
}
impl Binding {
pub(super) fn visibility(&self) -> &Visibility {
match self {
Binding::Import { visibility, .. } | Binding::Alias { visibility, .. } => visibility,
}
}
}
pub(super) struct Glob {
pub written: String,
pub visibility: Visibility,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(super) enum DeclKind {
Module(String),
ExternCrate {
target: String,
gated: bool,
},
Item(ItemKeyword),
}
#[derive(Clone, Debug)]
pub(super) struct Declaration {
pub type_ns: bool,
pub value_ns: bool,
pub visibility: Visibility,
pub kind: DeclKind,
}
impl Declaration {
pub(super) fn in_namespace(&self, ns: Namespace) -> bool {
match ns {
Namespace::Type => self.type_ns,
Namespace::Value => self.value_ns,
Namespace::Either => self.type_ns || self.value_ns,
}
}
}
pub(super) struct Scope {
pub parent: Option<u32>,
pub kind: ScopeKind,
pub module: String,
pub bindings: BTreeMap<String, Vec<Binding>>,
pub declarations: BTreeMap<String, Vec<Declaration>>,
pub globs: Vec<Glob>,
pub ungated: BTreeMap<String, (bool, bool)>,
pub macro_items: bool,
}
impl Scope {
fn mark_ungated(&mut self, name: &str, type_ns: bool, value_ns: bool) {
let held = self.ungated.entry(name.to_string()).or_default();
held.0 |= type_ns;
held.1 |= value_ns;
}
}
pub(super) struct ScopeTable {
pub scopes: Vec<Scope>,
pub table: usize,
scope_of: Vec<u32>,
refusal: Option<String>,
}
#[cfg(test)]
thread_local! {
static TABLE_BUILDS: std::cell::RefCell<std::collections::HashMap<(String, usize), usize>> =
std::cell::RefCell::new(std::collections::HashMap::new());
}
#[cfg(test)]
pub(crate) fn take_table_builds() -> std::collections::HashMap<(String, usize), usize> {
TABLE_BUILDS.with(|builds| std::mem::take(&mut *builds.borrow_mut()))
}
struct Frame {
close: usize,
kind: GroupKind,
outer: (u32, Option<u32>, bool, Option<u32>),
}
fn push_scope(
scopes: &mut Vec<Scope>,
parent: Option<u32>,
kind: ScopeKind,
module: String,
) -> u32 {
let id = u32::try_from(scopes.len()).expect("scope table exceeds u32");
scopes.push(Scope {
parent,
kind,
module,
bindings: BTreeMap::new(),
declarations: BTreeMap::new(),
globs: Vec::new(),
ungated: BTreeMap::new(),
macro_items: false,
});
id
}
impl ScopeTable {
pub(super) fn build(tree: &TokenTree, file_module: &str, table: usize) -> Self {
Self::build_naming(tree, file_module, table, &block_modules(tree))
}
fn build_naming(
tree: &TokenTree,
file_module: &str,
table: usize,
blocks: &[super::item_head::BlockModule],
) -> Self {
#[cfg(test)]
TABLE_BUILDS.with(|builds| {
*builds
.borrow_mut()
.entry((file_module.to_string(), table))
.or_default() += 1;
});
let mut scopes = Vec::new();
push_scope(
&mut scopes,
None,
ScopeKind::Module,
file_module.to_string(),
);
let n = tree.len();
let mut scope_of = vec![0u32; n + 1];
let mut record_of: Vec<Option<u32>> = vec![Some(0); n + 1];
let mut macro_use_of: Vec<Option<u32>> = vec![None; n + 1];
let mut frames: Vec<Frame> = Vec::new();
let (mut scope, mut record, mut in_macro, mut macro_block) =
(0u32, Some(0u32), false, None::<u32>);
let labels: BTreeMap<usize, &str> = blocks
.iter()
.flat_map(|block| {
std::iter::once(block.head.keyword_at)
.chain(block.head.body)
.map(|at| (at, block.label.as_str()))
})
.collect();
for i in 0..n {
if frames.last().is_some_and(|frame| frame.close == i) {
if let Some(frame) = frames.pop() {
(scope, record, in_macro, macro_block) = frame.outer;
}
}
scope_of[i] = scope;
record_of[i] = record.filter(|_| !in_macro);
macro_use_of[i] = macro_block;
let Kind::Open(_) = tree.kind(i) else {
continue;
};
let kind = classify_group(tree, i, frames.last().map(|frame| &frame.kind));
let outer = (scope, record, in_macro, macro_block);
match &kind {
GroupKind::ModuleBody(name) => {
let enclosing = &scopes[scope as usize];
let module = match enclosing.kind {
ScopeKind::Module => format!("{}::{name}", enclosing.module),
ScopeKind::Block => match labels.get(&i) {
Some(label) => format!("{}::{label}::{name}", enclosing.module),
None => format!(
"{}::{}::{name}",
enclosing.module,
block_segment(table, scope)
),
},
};
scope = push_scope(&mut scopes, Some(scope), ScopeKind::Module, module);
record = Some(scope);
macro_block = in_macro.then_some(scope);
}
GroupKind::Block => {
let module = scopes[scope as usize].module.clone();
scope = push_scope(&mut scopes, Some(scope), ScopeKind::Block, module);
record = Some(scope);
macro_block = in_macro.then_some(scope);
}
GroupKind::MemberBody | GroupKind::UseGroup => {
record = None;
macro_block = None;
}
GroupKind::MacroBody => {
in_macro = true;
macro_block = None;
}
GroupKind::ExternBlock
| GroupKind::CfgIf
| GroupKind::CfgArm
| GroupKind::Other => {}
}
frames.push(Frame {
close: tree.partner(i),
kind,
outer,
});
}
scope_of[n] = scope;
record_of[n] = record.filter(|_| !in_macro);
macro_use_of[n] = macro_block;
let mut built = ScopeTable {
scopes,
table,
scope_of,
refusal: None,
};
built.record_uses(tree, &record_of, ¯o_use_of);
built.record_items(tree, &record_of, &labels);
built
}
pub(super) fn scope_at(&self, at: usize) -> u32 {
self.scope_of[at.min(self.scope_of.len() - 1)]
}
pub(super) fn refusal(&self) -> Option<&str> {
self.refusal.as_deref()
}
pub(super) fn globs(&self) -> impl Iterator<Item = (u32, &str)> {
self.scopes.iter().enumerate().flat_map(|(id, scope)| {
let id = u32::try_from(id).expect("scope table exceeds u32");
scope
.globs
.iter()
.map(move |glob| (id, glob.written.as_str()))
})
}
fn record_uses(
&mut self,
tree: &TokenTree,
record_of: &[Option<u32>],
macro_use_of: &[Option<u32>],
) {
let in_scopes = use_statements(tree)
.into_iter()
.map(|statement| (record_of[statement.at], statement));
let in_macro_blocks = macro_use_statements(tree)
.into_iter()
.filter(|statement| statement.leaves.is_ok())
.map(|statement| (macro_use_of[statement.at], statement));
for (record, statement) in in_scopes.chain(in_macro_blocks) {
let Some(scope) = record else {
continue;
};
let leaves = match statement.leaves {
Ok(leaves) => leaves,
Err(refusal) => {
self.refusal.get_or_insert(refusal);
continue;
}
};
let gated = may_be_cfg_gated(
tree,
item_at_keyword(tree, statement.at).map_or(statement.at, |head| head.start),
);
let entry = &mut self.scopes[scope as usize];
for leaf in leaves {
let (path, binds, module_only) = match leaf {
UseLeaf::Name { path, binds } => (path, binds, false),
UseLeaf::SelfLeaf { module, binds } => (module, binds, true),
UseLeaf::Glob(base) => {
entry.globs.push(Glob {
written: base,
visibility: statement.visibility.clone(),
});
continue;
}
UseLeaf::Empty(_) => continue,
};
if !gated {
entry.mark_ungated(&binds, true, true);
}
entry
.bindings
.entry(binds)
.or_default()
.push(Binding::Import {
written: path,
visibility: statement.visibility.clone(),
module_only,
});
}
}
}
fn record_items(
&mut self,
tree: &TokenTree,
record_of: &[Option<u32>],
labels: &BTreeMap<usize, &str>,
) {
for (i, &record) in record_of.iter().enumerate().take(tree.len()) {
if let Node::Macro { name, open, .. } = tree.node_at(i) {
if macro_group_kind(tree, open) == Some(GroupKind::MacroBody)
&& tree.text(name) != "macro_rules"
&& macro_stands_as_an_item(tree, name)
{
if let Some(scope) = record {
self.scopes[scope as usize].macro_items = true;
}
}
continue;
}
if !matches!(tree.kind(i), Kind::Keyword | Kind::Ident) {
continue;
}
let Some(scope) = record else {
continue;
};
let Some(head) = item_at_keyword(tree, i) else {
continue;
};
let gated = may_be_cfg_gated(tree, head.start);
if head.keyword == ItemKeyword::ExternCrate {
self.record_extern_crate(tree, i, scope, head.visibility, gated);
continue;
}
let Some(name_at) = head.name else {
continue;
};
let (type_ns, value_ns) = match head.keyword {
ItemKeyword::Struct => (true, head.value_struct),
ItemKeyword::Enum
| ItemKeyword::Union
| ItemKeyword::Trait
| ItemKeyword::Mod
| ItemKeyword::Type => (true, false),
ItemKeyword::Fn | ItemKeyword::Const | ItemKeyword::Static => (false, true),
ItemKeyword::Impl
| ItemKeyword::Use
| ItemKeyword::ExternCrate
| ItemKeyword::ExternBlock
| ItemKeyword::MacroRules => continue,
};
let name = tree.text(name_at).to_string();
let label = match (head.keyword, self.scopes[scope as usize].kind) {
(ItemKeyword::Mod, ScopeKind::Block) => match labels.get(&head.keyword_at) {
Some(label) => Some(*label),
None => {
self.refusal.get_or_insert_with(|| {
format!(
"cannot judge the module `{name}` a block declares: the reading that names a \
block's modules gave it no name"
)
});
continue;
}
},
_ => None,
};
let entry = &mut self.scopes[scope as usize];
if head.keyword == ItemKeyword::Type {
let target = alias_target(tree, name_at + 1);
let aliased = target.is_some();
if let Some(written) = target {
if !gated {
entry.mark_ungated(&name, true, true);
}
entry
.bindings
.entry(name.clone())
.or_default()
.push(Binding::Alias {
written,
visibility: head.visibility.clone(),
});
}
if aliased && entry.kind == ScopeKind::Block {
continue;
}
}
let kind = match (head.keyword, label) {
(ItemKeyword::Mod, None) => DeclKind::Module(format!("{}::{name}", entry.module)),
(ItemKeyword::Mod, Some(label)) => {
DeclKind::Module(format!("{}::{label}::{name}", entry.module))
}
(keyword, _) => DeclKind::Item(keyword),
};
if !gated {
entry.mark_ungated(&name, type_ns, value_ns);
}
entry
.declarations
.entry(name)
.or_default()
.push(Declaration {
type_ns,
value_ns,
visibility: head.visibility,
kind,
});
}
}
fn record_extern_crate(
&mut self,
tree: &TokenTree,
at: usize,
scope: u32,
visibility: Visibility,
gated: bool,
) {
let named = at + 2;
if named >= tree.len() {
return;
}
let target = if tree.is(named, "self") {
"crate".to_string()
} else {
tree.text(named).to_string()
};
let binds = if tree.is(named + 1, "as") {
if !tree.is_word(named + 2) {
return;
}
tree.text(named + 2).to_string()
} else {
tree.text(named).to_string()
};
if !gated {
self.scopes[scope as usize].mark_ungated(&binds, true, false);
}
self.scopes[scope as usize]
.declarations
.entry(binds)
.or_default()
.push(Declaration {
type_ns: true,
value_ns: false,
visibility,
kind: DeclKind::ExternCrate { target, gated },
});
}
}
fn alias_target(tree: &TokenTree, from: usize) -> Option<String> {
let mut k = from;
let mut past_eq = false;
if tree.is(k, "<") {
let close = super::item_head::angle_group_end(tree, k)?;
past_eq = tree.is(close, ">=") || tree.is(close, ">>=");
k = close + 1;
}
if !past_eq {
while k < tree.len() && !tree.is(k, "=") {
if tree.is(k, ";") || matches!(tree.kind(k), Kind::Open(_) | Kind::Close(_)) {
return None;
}
k += 1;
}
k += 1;
}
let mut closers = Vec::new();
loop {
if tree.kind(k) == Kind::Open(Delimiter::Parenthesis) {
closers.push(tree.partner(k));
k += 1;
} else if tree.is(k, "&") || tree.is(k, "&&") {
k += 1;
if tree.kind(k) == Kind::Lifetime {
k += 1;
}
if tree.is(k, "mut") {
k += 1;
}
} else if tree.is(k, "*") && (tree.is(k + 1, "const") || tree.is(k + 1, "mut")) {
k += 2;
} else {
break;
}
}
let rooted = tree.is(k, "::");
let head = if rooted { k + 1 } else { k };
if !heads_a_path(tree, head) {
return None;
}
let run = path_run(tree, head);
let end = if super::item_head::opens_an_angle_group(tree, run.end) {
super::item_head::angle_group_end(tree, run.end)? + 1
} else {
run.end
};
if !closers
.iter()
.rev()
.enumerate()
.all(|(depth, close)| end + depth == *close)
{
return None;
}
let joined = run.segments.join("::");
Some(if rooted {
format!("::{joined}")
} else {
joined
})
}
#[cfg(test)]
mod tests {
use super::super::token_tree::Edition;
use super::*;
#[test]
fn a_block_module_left_unnamed_is_refused() {
let tree = TokenTree::lex("fn f() { mod m; }\n", Edition::Rust2021);
let table = ScopeTable::build_naming(&tree, "crate", 0, &[]);
let refusal = table
.refusal()
.expect("an unnamed block module must refuse its file");
assert!(
refusal.contains("cannot judge the module `m` a block declares"),
"{refusal}"
);
assert!(ScopeTable::build(&tree, "crate", 0).refusal().is_none());
}
}