use super::db::{CrateId, Edition, FileId, ItemDb, ModuleId, SourceDb, VisKind};
use super::mod_builder::Builder;
use crate::sema_fn::norm_ident;
use crate::tree::SyntaxTree;
pub const MOD_FLAG_INLINE: u8 = 1 << 0;
pub const MOD_FLAG_UNRESOLVED: u8 = 1 << 1;
pub const MOD_FLAG_NO_IMPLICIT_PRELUDE: u8 = 1 << 2;
pub const MOD_FLAG_MACRO_USE: u8 = 1 << 3;
pub const MOD_FLAG_NON_MOD_RS: u8 = 1 << 4;
#[derive(Clone, Copy, Debug)]
pub struct ModuleData {
pub id: ModuleId,
pub parent: ModuleId,
pub file: FileId,
pub decl_file: FileId,
pub inline_node: u32,
pub decl_start: u32,
pub decl_end: u32,
pub(super) name_start: u32,
pub(super) name_end: u32,
pub(super) crate_path_start: u32,
pub(super) crate_path_end: u32,
pub(super) dir_start: u32,
pub(super) dir_end: u32,
pub(super) children_start: u32,
pub(super) children_len: u32,
pub vis: VisKind,
pub flags: u8,
}
impl ModuleData {
#[inline]
pub const fn is_inline(&self) -> bool {
(self.flags & MOD_FLAG_INLINE) != 0
}
#[inline]
pub const fn is_unresolved(&self) -> bool {
(self.flags & MOD_FLAG_UNRESOLVED) != 0
}
#[inline]
pub const fn has_no_implicit_prelude(&self) -> bool {
(self.flags & MOD_FLAG_NO_IMPLICIT_PRELUDE) != 0
}
#[inline]
pub const fn is_non_mod_rs(&self) -> bool {
(self.flags & MOD_FLAG_NON_MOD_RS) != 0
}
}
#[derive(Clone, Copy, Debug)]
pub struct ModChild {
pub child: ModuleId,
pub(super) name_start: u32,
pub(super) name_end: u32,
pub vis: VisKind,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ModDiag {
DisabledCrateRoot { file: FileId },
UnresolvedModFile { parent: ModuleId, name: String, tried_primary: String, tried_mod_rs: Option<String> },
AmbiguousModFile { parent: ModuleId, name: String, chosen: FileId, alternate: FileId },
CyclicModFile { parent: ModuleId, name: String, file: FileId },
DuplicateModDecl { parent: ModuleId, name: String },
}
#[derive(Clone, Debug)]
pub struct ModuleTree {
pub krate: CrateId,
pub edition: Edition,
pub no_std: bool,
pub no_core: bool,
pub modules: Vec<ModuleData>,
pub(super) children: Vec<ModChild>,
pub(super) file_to_modules: Vec<(FileId, ModuleId)>,
pub(super) strings: String,
pub diagnostics: Vec<ModDiag>,
}
impl ModuleTree {
pub fn build(db: &dyn ItemDb, sources: &dyn SourceDb, krate: CrateId, root_file: FileId) -> Self {
Self::build_cached(db, sources, krate, root_file, |_| None)
}
pub fn build_cached<'t>(db: &dyn ItemDb, sources: &dyn SourceDb, krate: CrateId, root_file: FileId, mut get_tree: impl FnMut(FileId) -> Option<&'t SyntaxTree<'t>>) -> Self {
Builder::new(db, sources, krate, Some(&mut get_tree)).run(root_file)
}
pub(super) fn build_capturing_trees<'s>(
db: &'s dyn ItemDb,
sources: &'s dyn SourceDb,
krate: CrateId,
root_file: FileId,
) -> (Self, std::collections::HashMap<FileId, SyntaxTree<'s>>) {
Builder::<'s, 's>::new(db, sources, krate, None).run_capturing(root_file)
}
#[inline]
pub fn module(&self, id: ModuleId) -> &ModuleData {
&self.modules[id.0 as usize]
}
#[inline]
pub fn name(&self, id: ModuleId) -> &str {
let m = self.module(id);
&self.strings[m.name_start as usize..m.name_end as usize]
}
#[inline]
pub fn crate_path(&self, id: ModuleId) -> &str {
let m = self.module(id);
&self.strings[m.crate_path_start as usize..m.crate_path_end as usize]
}
#[inline]
pub fn dir_path(&self, id: ModuleId) -> &str {
let m = self.module(id);
&self.strings[m.dir_start as usize..m.dir_end as usize]
}
#[inline]
pub fn attr_base_dir(&self, id: ModuleId) -> &str {
let m = self.module(id);
let dir = &self.strings[m.dir_start as usize..m.dir_end as usize];
if m.is_non_mod_rs() {
parent_dir_str(dir)
} else {
dir
}
}
#[inline]
pub fn children(&self, id: ModuleId) -> &[ModChild] {
let m = self.module(id);
let s = m.children_start as usize;
&self.children[s..s + m.children_len as usize]
}
#[inline]
pub fn child_name(&self, edge: &ModChild) -> &str {
&self.strings[edge.name_start as usize..edge.name_end as usize]
}
pub fn find_child(&self, parent: ModuleId, name: &str) -> Option<ModuleId> {
let target = norm_ident(name);
self.children(parent).iter().find(|e| self.child_name(e) == target).map(|e| e.child)
}
pub fn find_by_path(&self, path: &str) -> Option<ModuleId> {
if self.modules.is_empty() {
return None;
}
let mut cur = ModuleId::ROOT;
for seg in path.split("::").filter(|s| !s.is_empty()) {
if seg == "crate" {
cur = ModuleId::ROOT;
} else {
cur = self.find_child(cur, seg)?;
}
}
Some(cur)
}
pub fn modules_for_file(&self, file: FileId) -> impl Iterator<Item = ModuleId> + '_ {
let start = self.file_to_modules.partition_point(|&(f, _)| f < file);
let end = self.file_to_modules[start..].partition_point(|&(f, _)| f == file) + start;
self.file_to_modules[start..end].iter().map(|&(_, m)| m)
}
pub fn module_at_offset(&self, file: FileId, byte_offset: u32) -> Option<ModuleId> {
let mut best: Option<ModuleId> = None;
for m_id in self.modules_for_file(file) {
let m = self.module(m_id);
if !m.is_inline() || m.file != file {
if best.is_none() {
best = Some(m_id);
}
} else if m.decl_start <= byte_offset && byte_offset <= m.decl_end {
best = Some(m_id);
}
}
best
}
}
#[inline]
pub(super) fn parent_dir_str(path: &str) -> &str {
match path.rfind('/') {
Some(0) => "/",
Some(idx) => &path[..idx],
None => "",
}
}
#[inline]
fn push_norm_segment(buf: &mut String, part: &str, is_abs: bool) {
match part {
"" | "." => {}
".." => {
if is_abs {
if buf.len() > 1 {
let slash = buf.rfind('/').unwrap_or(0);
buf.truncate(slash.max(1));
}
return;
}
if let Some(slash) = buf.rfind('/') {
if &buf[slash + 1..] != ".." {
buf.truncate(slash);
return;
}
} else if !buf.is_empty() && buf != ".." {
buf.clear();
return;
}
if !buf.is_empty() {
buf.push('/');
}
buf.push_str("..");
}
normal => {
if !buf.is_empty() && buf != "/" {
buf.push('/');
}
buf.push_str(normal);
}
}
}
pub fn join_norm_into(out: &mut String, base_dir: &str, rel: &str, suffix: &str) {
out.clear();
let rel_is_abs = rel.starts_with('/');
let is_abs = rel_is_abs || base_dir.starts_with('/');
if is_abs {
out.push('/');
}
if !rel_is_abs {
for part in base_dir.split(['/', '\\']) {
push_norm_segment(out, part, is_abs);
}
}
let mut rel_it = rel.split(['/', '\\']).peekable();
while let Some(part) = rel_it.next() {
if rel_it.peek().is_none() && !suffix.is_empty() {
if suffix.starts_with('/') {
push_norm_segment(out, part, is_abs);
for sp in suffix.split('/') {
push_norm_segment(out, sp, is_abs);
}
} else {
push_norm_segment(out, part, is_abs);
out.push_str(suffix);
}
} else {
push_norm_segment(out, part, is_abs);
}
}
}
pub fn join_norm_path(base_dir: &str, rel: &str) -> String {
let mut out = String::with_capacity(base_dir.len() + rel.len() + 1);
join_norm_into(&mut out, base_dir, rel, "");
out
}