2018-11-27 18:25:20 -06:00
|
|
|
use std::sync::Arc;
|
2018-10-25 02:57:55 -05:00
|
|
|
|
2018-12-08 16:02:53 -06:00
|
|
|
use rustc_hash::{FxHashSet, FxHashMap};
|
|
|
|
use ra_syntax::SmolStr;
|
2018-10-31 15:41:43 -05:00
|
|
|
use salsa;
|
2018-10-25 02:57:55 -05:00
|
|
|
|
2018-11-27 18:25:20 -06:00
|
|
|
use crate::file_resolver::FileResolverImp;
|
2018-10-25 09:52:50 -05: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 07:01:18 -06:00
|
|
|
arena: FxHashMap<CrateId, CrateData>,
|
2018-10-25 09:52:50 -05:00
|
|
|
}
|
|
|
|
|
2018-12-05 07:01:18 -06:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
struct CrateData {
|
|
|
|
file_id: FileId,
|
2018-12-08 15:51:06 -06:00
|
|
|
dependencies: Vec<Dependency>,
|
2018-12-05 07:01:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CrateData {
|
|
|
|
fn new(file_id: FileId) -> CrateData {
|
|
|
|
CrateData {
|
|
|
|
file_id,
|
2018-12-08 15:51:06 -06:00
|
|
|
dependencies: Vec::new(),
|
2018-12-05 07:01:18 -06:00
|
|
|
}
|
2018-10-25 09:52:50 -05:00
|
|
|
}
|
2018-12-05 07:01:18 -06:00
|
|
|
|
2018-12-08 16:02:53 -06:00
|
|
|
fn add_dep(&mut self, name: SmolStr, crate_id: CrateId) {
|
|
|
|
self.dependencies.push(Dependency { name, crate_id })
|
2018-12-05 07:01:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub struct Dependency {
|
2018-12-08 15:51:06 -06:00
|
|
|
crate_id: CrateId,
|
2018-12-08 16:02:53 -06:00
|
|
|
name: SmolStr,
|
2018-12-08 15:51:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Dependency {
|
|
|
|
pub fn crate_id(&self) -> CrateId {
|
|
|
|
self.crate_id
|
|
|
|
}
|
2018-12-05 07:01:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CrateGraph {
|
2018-10-31 14:34:31 -05:00
|
|
|
pub fn add_crate_root(&mut self, file_id: FileId) -> CrateId {
|
2018-12-05 07:01:18 -06:00
|
|
|
let crate_id = CrateId(self.arena.len() as u32);
|
|
|
|
let prev = self.arena.insert(crate_id, CrateData::new(file_id));
|
2018-10-25 09:52:50 -05:00
|
|
|
assert!(prev.is_none());
|
|
|
|
crate_id
|
|
|
|
}
|
2018-12-08 14:16:11 -06:00
|
|
|
//FIXME: check that we don't have cycles here.
|
|
|
|
// Just a simple depth first search from `to` should work,
|
|
|
|
// the graph is small.
|
2018-12-08 16:02:53 -06:00
|
|
|
pub fn add_dep(&mut self, from: CrateId, name: SmolStr, to: CrateId) {
|
|
|
|
self.arena.get_mut(&from).unwrap().add_dep(name, to)
|
2018-12-05 07:01:18 -06:00
|
|
|
}
|
|
|
|
pub fn crate_root(&self, crate_id: CrateId) -> FileId {
|
|
|
|
self.arena[&crate_id].file_id
|
|
|
|
}
|
2018-11-27 18:25:20 -06:00
|
|
|
pub fn crate_id_for_crate_root(&self, file_id: FileId) -> Option<CrateId> {
|
|
|
|
let (&crate_id, _) = self
|
2018-12-05 07:01:18 -06:00
|
|
|
.arena
|
2018-11-27 18:25:20 -06:00
|
|
|
.iter()
|
2018-12-05 07:01:18 -06:00
|
|
|
.find(|(_crate_id, data)| data.file_id == file_id)?;
|
2018-11-27 18:25:20 -06:00
|
|
|
Some(crate_id)
|
2018-11-26 15:12:43 -06:00
|
|
|
}
|
2018-12-08 15:51:06 -06:00
|
|
|
pub fn dependencies<'a>(
|
|
|
|
&'a self,
|
|
|
|
crate_id: CrateId,
|
|
|
|
) -> impl Iterator<Item = &'a Dependency> + 'a {
|
|
|
|
self.arena[&crate_id].dependencies.iter()
|
|
|
|
}
|
2018-10-25 09:52:50 -05:00
|
|
|
}
|
2018-10-25 02:57:55 -05:00
|
|
|
|
|
|
|
salsa::query_group! {
|
2018-11-27 18:25:20 -06:00
|
|
|
pub trait FilesDatabase: salsa::Database {
|
2018-10-25 02:57:55 -05: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 13:05:14 -05:00
|
|
|
type LibrariesQuery;
|
2018-10-25 02:57:55 -05:00
|
|
|
storage input;
|
|
|
|
}
|
|
|
|
fn crate_graph() -> Arc<CrateGraph> {
|
|
|
|
type CrateGraphQuery;
|
|
|
|
storage input;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-30 16:59:43 -05:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
2018-11-27 18:25:20 -06:00
|
|
|
pub struct SourceRootId(pub u32);
|
2018-10-25 02:57:55 -05:00
|
|
|
|
2018-10-30 16:59:43 -05:00
|
|
|
#[derive(Default, Clone, Debug, PartialEq, Eq)]
|
2018-11-27 18:25:20 -06:00
|
|
|
pub struct SourceRoot {
|
|
|
|
pub file_resolver: FileResolverImp,
|
|
|
|
pub files: FxHashSet<FileId>,
|
2018-10-25 02:57:55 -05:00
|
|
|
}
|
|
|
|
|
2018-11-27 18:25:20 -06:00
|
|
|
pub const WORKSPACE: SourceRootId = SourceRootId(0);
|