2018-11-28 00:25:20 +00:00
|
|
|
use std::sync::Arc;
|
2018-10-25 07:57:55 +00:00
|
|
|
|
2018-10-25 14:52:50 +00:00
|
|
|
use rustc_hash::FxHashMap;
|
2018-10-31 20:41:43 +00:00
|
|
|
use rustc_hash::FxHashSet;
|
|
|
|
use salsa;
|
2018-10-25 07:57:55 +00:00
|
|
|
|
2018-11-28 00:25:20 +00:00
|
|
|
use crate::file_resolver::FileResolverImp;
|
2018-10-25 14:52:50 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
|
|
|
pub struct FileId(pub u32);
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
|
|
|
pub struct CrateId(pub u32);
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
|
|
|
pub struct CrateGraph {
|
2018-12-05 12:31:40 +00:00
|
|
|
crate_roots: FxHashMap<CrateId, FileId>,
|
2018-10-25 14:52:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CrateGraph {
|
2018-11-28 00:25:20 +00:00
|
|
|
pub fn crate_root(&self, crate_id: CrateId) -> FileId {
|
|
|
|
self.crate_roots[&crate_id]
|
2018-10-25 14:52:50 +00:00
|
|
|
}
|
2018-10-31 19:34:31 +00:00
|
|
|
pub fn add_crate_root(&mut self, file_id: FileId) -> CrateId {
|
2018-10-25 14:52:50 +00:00
|
|
|
let crate_id = CrateId(self.crate_roots.len() as u32);
|
|
|
|
let prev = self.crate_roots.insert(crate_id, file_id);
|
|
|
|
assert!(prev.is_none());
|
|
|
|
crate_id
|
|
|
|
}
|
2018-11-28 00:25:20 +00:00
|
|
|
pub fn crate_id_for_crate_root(&self, file_id: FileId) -> Option<CrateId> {
|
|
|
|
let (&crate_id, _) = self
|
|
|
|
.crate_roots
|
|
|
|
.iter()
|
|
|
|
.find(|(_crate_id, &root_id)| root_id == file_id)?;
|
|
|
|
Some(crate_id)
|
2018-11-26 21:12:43 +00:00
|
|
|
}
|
2018-10-25 14:52:50 +00:00
|
|
|
}
|
2018-10-25 07:57:55 +00:00
|
|
|
|
|
|
|
salsa::query_group! {
|
2018-11-28 00:25:20 +00:00
|
|
|
pub trait FilesDatabase: salsa::Database {
|
2018-10-25 07:57:55 +00:00
|
|
|
fn file_text(file_id: FileId) -> Arc<String> {
|
|
|
|
type FileTextQuery;
|
|
|
|
storage input;
|
|
|
|
}
|
|
|
|
fn file_source_root(file_id: FileId) -> SourceRootId {
|
|
|
|
type FileSourceRootQuery;
|
|
|
|
storage input;
|
|
|
|
}
|
|
|
|
fn source_root(id: SourceRootId) -> Arc<SourceRoot> {
|
|
|
|
type SourceRootQuery;
|
|
|
|
storage input;
|
|
|
|
}
|
|
|
|
fn libraries() -> Arc<Vec<SourceRootId>> {
|
2018-10-31 18:05:14 +00:00
|
|
|
type LibrariesQuery;
|
2018-10-25 07:57:55 +00:00
|
|
|
storage input;
|
|
|
|
}
|
|
|
|
fn crate_graph() -> Arc<CrateGraph> {
|
|
|
|
type CrateGraphQuery;
|
|
|
|
storage input;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-30 21:59:43 +00:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
2018-11-28 00:25:20 +00:00
|
|
|
pub struct SourceRootId(pub u32);
|
2018-10-25 07:57:55 +00:00
|
|
|
|
2018-10-30 21:59:43 +00:00
|
|
|
#[derive(Default, Clone, Debug, PartialEq, Eq)]
|
2018-11-28 00:25:20 +00:00
|
|
|
pub struct SourceRoot {
|
|
|
|
pub file_resolver: FileResolverImp,
|
|
|
|
pub files: FxHashSet<FileId>,
|
2018-10-25 07:57:55 +00:00
|
|
|
}
|
|
|
|
|
2018-11-28 00:25:20 +00:00
|
|
|
pub const WORKSPACE: SourceRootId = SourceRootId(0);
|